File: | var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h |
Warning: | line 1320, column 7 Called C++ object pointer is null |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */ | |||
2 | /* This Source Code Form is subject to the terms of the Mozilla Public | |||
3 | * License, v. 2.0. If a copy of the MPL was not distributed with this | |||
4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | |||
5 | ||||
6 | #include "mozilla/devtools/DominatorTree.h" | |||
7 | #include "mozilla/dom/DominatorTreeBinding.h" | |||
8 | #include "mozilla/ErrorResult.h" | |||
9 | ||||
10 | namespace mozilla { | |||
11 | namespace devtools { | |||
12 | ||||
13 | dom::Nullable<uint64_t> DominatorTree::GetRetainedSize(uint64_t aNodeId, | |||
14 | ErrorResult& aRv) { | |||
15 | JS::ubi::Node::Id id(aNodeId); | |||
16 | auto node = mHeapSnapshot->getNodeById(id); | |||
17 | if (node.isNothing()) return dom::Nullable<uint64_t>(); | |||
18 | ||||
19 | auto mallocSizeOf = GetCurrentThreadDebuggerMallocSizeOf(); | |||
20 | JS::ubi::Node::Size size = 0; | |||
21 | if (!mDominatorTree.getRetainedSize(*node, mallocSizeOf, size)) { | |||
22 | aRv.Throw(NS_ERROR_OUT_OF_MEMORY); | |||
23 | return dom::Nullable<uint64_t>(); | |||
24 | } | |||
25 | ||||
26 | MOZ_ASSERT(size != 0,do { static_assert( mozilla::detail::AssertionConditionType< decltype(size != 0)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(size != 0))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("size != 0" " (" "The node should not have been unknown since we got it from the " "heap snapshot." ")", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/DominatorTree.cpp" , 28); AnnotateMozCrashReason("MOZ_ASSERT" "(" "size != 0" ") (" "The node should not have been unknown since we got it from the " "heap snapshot." ")"); do { *((volatile int*)__null) = 28; __attribute__ ((nomerge)) ::abort(); } while (false); } } while (false) | |||
27 | "The node should not have been unknown since we got it from the "do { static_assert( mozilla::detail::AssertionConditionType< decltype(size != 0)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(size != 0))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("size != 0" " (" "The node should not have been unknown since we got it from the " "heap snapshot." ")", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/DominatorTree.cpp" , 28); AnnotateMozCrashReason("MOZ_ASSERT" "(" "size != 0" ") (" "The node should not have been unknown since we got it from the " "heap snapshot." ")"); do { *((volatile int*)__null) = 28; __attribute__ ((nomerge)) ::abort(); } while (false); } } while (false) | |||
28 | "heap snapshot.")do { static_assert( mozilla::detail::AssertionConditionType< decltype(size != 0)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(size != 0))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("size != 0" " (" "The node should not have been unknown since we got it from the " "heap snapshot." ")", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/DominatorTree.cpp" , 28); AnnotateMozCrashReason("MOZ_ASSERT" "(" "size != 0" ") (" "The node should not have been unknown since we got it from the " "heap snapshot." ")"); do { *((volatile int*)__null) = 28; __attribute__ ((nomerge)) ::abort(); } while (false); } } while (false); | |||
29 | return dom::Nullable<uint64_t>(size); | |||
30 | } | |||
31 | ||||
32 | struct NodeAndRetainedSize { | |||
33 | JS::ubi::Node mNode; | |||
34 | JS::ubi::Node::Size mSize; | |||
35 | ||||
36 | NodeAndRetainedSize(const JS::ubi::Node& aNode, JS::ubi::Node::Size aSize) | |||
37 | : mNode(aNode), mSize(aSize) {} | |||
38 | ||||
39 | struct Comparator { | |||
40 | static bool Equals(const NodeAndRetainedSize& aLhs, | |||
41 | const NodeAndRetainedSize& aRhs) { | |||
42 | return aLhs.mSize == aRhs.mSize; | |||
43 | } | |||
44 | ||||
45 | static bool LessThan(const NodeAndRetainedSize& aLhs, | |||
46 | const NodeAndRetainedSize& aRhs) { | |||
47 | // Use > because we want to sort from greatest to least retained size. | |||
48 | return aLhs.mSize > aRhs.mSize; | |||
49 | } | |||
50 | }; | |||
51 | }; | |||
52 | ||||
53 | void DominatorTree::GetImmediatelyDominated( | |||
54 | uint64_t aNodeId, dom::Nullable<nsTArray<uint64_t>>& aOutResult, | |||
55 | ErrorResult& aRv) { | |||
56 | MOZ_ASSERT(aOutResult.IsNull())do { static_assert( mozilla::detail::AssertionConditionType< decltype(aOutResult.IsNull())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(aOutResult.IsNull()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("aOutResult.IsNull()" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/DominatorTree.cpp" , 56); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aOutResult.IsNull()" ")"); do { *((volatile int*)__null) = 56; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
57 | ||||
58 | JS::ubi::Node::Id id(aNodeId); | |||
59 | Maybe<JS::ubi::Node> node = mHeapSnapshot->getNodeById(id); | |||
60 | if (node.isNothing()) return; | |||
61 | ||||
62 | // Get all immediately dominated nodes and their retained sizes. | |||
63 | MallocSizeOf mallocSizeOf = GetCurrentThreadDebuggerMallocSizeOf(); | |||
64 | Maybe<JS::ubi::DominatorTree::DominatedSetRange> range = | |||
65 | mDominatorTree.getDominatedSet(*node); | |||
66 | MOZ_ASSERT(do { static_assert( mozilla::detail::AssertionConditionType< decltype(range.isSome())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(range.isSome()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("range.isSome()" " (" "The node should be known, since we got it from the heap snapshot." ")", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/DominatorTree.cpp" , 68); AnnotateMozCrashReason("MOZ_ASSERT" "(" "range.isSome()" ") (" "The node should be known, since we got it from the heap snapshot." ")"); do { *((volatile int*)__null) = 68; __attribute__((nomerge )) ::abort(); } while (false); } } while (false) | |||
67 | range.isSome(),do { static_assert( mozilla::detail::AssertionConditionType< decltype(range.isSome())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(range.isSome()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("range.isSome()" " (" "The node should be known, since we got it from the heap snapshot." ")", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/DominatorTree.cpp" , 68); AnnotateMozCrashReason("MOZ_ASSERT" "(" "range.isSome()" ") (" "The node should be known, since we got it from the heap snapshot." ")"); do { *((volatile int*)__null) = 68; __attribute__((nomerge )) ::abort(); } while (false); } } while (false) | |||
68 | "The node should be known, since we got it from the heap snapshot.")do { static_assert( mozilla::detail::AssertionConditionType< decltype(range.isSome())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(range.isSome()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("range.isSome()" " (" "The node should be known, since we got it from the heap snapshot." ")", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/DominatorTree.cpp" , 68); AnnotateMozCrashReason("MOZ_ASSERT" "(" "range.isSome()" ") (" "The node should be known, since we got it from the heap snapshot." ")"); do { *((volatile int*)__null) = 68; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
69 | size_t length = range->length(); | |||
70 | nsTArray<NodeAndRetainedSize> dominatedNodes(length); | |||
71 | for (const JS::ubi::Node& dominatedNode : *range) { | |||
72 | JS::ubi::Node::Size retainedSize = 0; | |||
73 | if (NS_WARN_IF(!mDominatorTree.getRetainedSize(dominatedNode, mallocSizeOf,NS_warn_if_impl(!mDominatorTree.getRetainedSize(dominatedNode , mallocSizeOf, retainedSize), "!mDominatorTree.getRetainedSize(dominatedNode, mallocSizeOf, retainedSize)" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/DominatorTree.cpp" , 74) | |||
74 | retainedSize))NS_warn_if_impl(!mDominatorTree.getRetainedSize(dominatedNode , mallocSizeOf, retainedSize), "!mDominatorTree.getRetainedSize(dominatedNode, mallocSizeOf, retainedSize)" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/DominatorTree.cpp" , 74)) { | |||
75 | aRv.Throw(NS_ERROR_OUT_OF_MEMORY); | |||
76 | return; | |||
77 | } | |||
78 | MOZ_ASSERT(retainedSize != 0,do { static_assert( mozilla::detail::AssertionConditionType< decltype(retainedSize != 0)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(retainedSize != 0))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("retainedSize != 0" " (" "retainedSize should not be zero since we know the node is in " "the dominator tree." ")", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/DominatorTree.cpp" , 80); AnnotateMozCrashReason("MOZ_ASSERT" "(" "retainedSize != 0" ") (" "retainedSize should not be zero since we know the node is in " "the dominator tree." ")"); do { *((volatile int*)__null) = 80 ; __attribute__((nomerge)) ::abort(); } while (false); } } while (false) | |||
79 | "retainedSize should not be zero since we know the node is in "do { static_assert( mozilla::detail::AssertionConditionType< decltype(retainedSize != 0)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(retainedSize != 0))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("retainedSize != 0" " (" "retainedSize should not be zero since we know the node is in " "the dominator tree." ")", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/DominatorTree.cpp" , 80); AnnotateMozCrashReason("MOZ_ASSERT" "(" "retainedSize != 0" ") (" "retainedSize should not be zero since we know the node is in " "the dominator tree." ")"); do { *((volatile int*)__null) = 80 ; __attribute__((nomerge)) ::abort(); } while (false); } } while (false) | |||
80 | "the dominator tree.")do { static_assert( mozilla::detail::AssertionConditionType< decltype(retainedSize != 0)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(retainedSize != 0))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("retainedSize != 0" " (" "retainedSize should not be zero since we know the node is in " "the dominator tree." ")", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/DominatorTree.cpp" , 80); AnnotateMozCrashReason("MOZ_ASSERT" "(" "retainedSize != 0" ") (" "retainedSize should not be zero since we know the node is in " "the dominator tree." ")"); do { *((volatile int*)__null) = 80 ; __attribute__((nomerge)) ::abort(); } while (false); } } while (false); | |||
81 | ||||
82 | dominatedNodes.AppendElement( | |||
83 | NodeAndRetainedSize(dominatedNode, retainedSize)); | |||
84 | } | |||
85 | ||||
86 | // Sort them by retained size. | |||
87 | NodeAndRetainedSize::Comparator comparator; | |||
88 | dominatedNodes.Sort(comparator); | |||
89 | ||||
90 | // Fill the result with the nodes' ids. | |||
91 | JS::ubi::Node root = mDominatorTree.root(); | |||
92 | aOutResult.SetValue(nsTArray<uint64_t>(length)); | |||
93 | for (const NodeAndRetainedSize& entry : dominatedNodes) { | |||
94 | // The root dominates itself, but we don't want to expose that to JS. | |||
95 | if (entry.mNode == root) continue; | |||
96 | ||||
97 | aOutResult.Value().AppendElement(entry.mNode.identifier()); | |||
98 | } | |||
99 | } | |||
100 | ||||
101 | dom::Nullable<uint64_t> DominatorTree::GetImmediateDominator( | |||
102 | uint64_t aNodeId) const { | |||
103 | JS::ubi::Node::Id id(aNodeId); | |||
104 | Maybe<JS::ubi::Node> node = mHeapSnapshot->getNodeById(id); | |||
105 | if (node.isNothing()) return dom::Nullable<uint64_t>(); | |||
| ||||
106 | ||||
107 | JS::ubi::Node dominator = mDominatorTree.getImmediateDominator(*node); | |||
108 | if (!dominator || dominator == *node) return dom::Nullable<uint64_t>(); | |||
109 | ||||
110 | return dom::Nullable<uint64_t>(dominator.identifier()); | |||
111 | } | |||
112 | ||||
113 | /*** Cycle Collection Boilerplate | |||
114 | * *****************************************************************/ | |||
115 | ||||
116 | NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(DominatorTree, mParent, mHeapSnapshot)static_assert(std::is_base_of<nsWrapperCache, DominatorTree >::value, "Class should inherit nsWrapperCache"); DominatorTree ::cycleCollection DominatorTree::_cycleCollectorGlobal( nsCycleCollectionParticipant ::FlagMaybeSingleZoneJSHolder); void DominatorTree::cycleCollection ::Trace( void* p, const TraceCallbacks& aCallbacks, void* aClosure) { DominatorTree* tmp = DowncastCCParticipant<DominatorTree >(p); TraceWrapper(p, aCallbacks, aClosure); (void)tmp; } void DominatorTree::cycleCollection::TraceWrapper( void* p, const TraceCallbacks& aCallbacks, void* aClosure) { DominatorTree * tmp = DowncastCCParticipant<DominatorTree>(p); tmp-> TraceWrapper(aCallbacks, aClosure); } void DominatorTree::cycleCollection ::Unlink(void* p) { DominatorTree* tmp = DowncastCCParticipant <DominatorTree>(p); ImplCycleCollectionUnlink(tmp->mParent ); ImplCycleCollectionUnlink(tmp->mHeapSnapshot); tmp-> ReleaseWrapper(p); (void)tmp; } nsresult DominatorTree::cycleCollection ::TraverseNative( void* p, nsCycleCollectionTraversalCallback & cb) { DominatorTree* tmp = DowncastCCParticipant<DominatorTree >(p); cb.DescribeRefCountedNode(tmp->mRefCnt.get(), "DominatorTree" ); ImplCycleCollectionTraverse(cb, tmp->mParent, "mParent" , 0); ImplCycleCollectionTraverse(cb, tmp->mHeapSnapshot, "mHeapSnapshot" , 0); (void)tmp; return NS_OK; } | |||
117 | ||||
118 | NS_IMPL_CYCLE_COLLECTING_ADDREF(DominatorTree)MozExternalRefCountType DominatorTree::AddRef(void) { static_assert (!std::is_destructible_v<DominatorTree>, "Reference-counted class " "DominatorTree" " should not have a public destructor. " "Make this class's destructor non-public" ); do { static_assert( mozilla::detail::AssertionConditionType <decltype(int32_t(mRefCnt) >= 0)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(int32_t(mRefCnt) >= 0))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("int32_t(mRefCnt) >= 0" " (" "illegal refcnt" ")", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/DominatorTree.cpp" , 118); AnnotateMozCrashReason("MOZ_ASSERT" "(" "int32_t(mRefCnt) >= 0" ") (" "illegal refcnt" ")"); do { *((volatile int*)__null) = 118; __attribute__((nomerge)) ::abort(); } while (false); } } while (false); _mOwningThread.AssertOwnership("DominatorTree" " not thread-safe"); nsISupports* base = DominatorTree::cycleCollection ::Upcast(this); nsrefcnt count = mRefCnt.incr(base); NS_LogAddRef ((this), (count), ("DominatorTree"), (uint32_t)(sizeof(*this) )); return count; } | |||
119 | NS_IMPL_CYCLE_COLLECTING_RELEASE(DominatorTree)MozExternalRefCountType DominatorTree::Release(void) { do { static_assert ( mozilla::detail::AssertionConditionType<decltype(int32_t (mRefCnt) > 0)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(int32_t(mRefCnt) > 0))), 0 ))) { do { } while (false); MOZ_ReportAssertionFailure("int32_t(mRefCnt) > 0" " (" "dup release" ")", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/DominatorTree.cpp" , 119); AnnotateMozCrashReason("MOZ_ASSERT" "(" "int32_t(mRefCnt) > 0" ") (" "dup release" ")"); do { *((volatile int*)__null) = 119 ; __attribute__((nomerge)) ::abort(); } while (false); } } while (false); _mOwningThread.AssertOwnership("DominatorTree" " not thread-safe" ); nsISupports* base = DominatorTree::cycleCollection::Upcast (this); nsrefcnt count = mRefCnt.decr(base); NS_LogRelease((this ), (count), ("DominatorTree")); return count; } void DominatorTree ::DeleteCycleCollectable(void) { delete (this); } | |||
120 | ||||
121 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DominatorTree)nsresult DominatorTree::QueryInterface(const nsIID& aIID, void** aInstancePtr) { do { if (!(aInstancePtr)) { NS_DebugBreak (NS_DEBUG_ASSERTION, "QueryInterface requires a non-NULL destination!" , "aInstancePtr", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/DominatorTree.cpp" , 121); MOZ_PretendNoReturn(); } } while (0); nsISupports* foundInterface ; if (TopThreeWordsEquals( aIID, (nsXPCOMCycleCollectionParticipant ::COMTypeInfo<nsXPCOMCycleCollectionParticipant, void>:: kIID), (nsCycleCollectionISupports::COMTypeInfo<nsCycleCollectionISupports , void>::kIID)) && (LowWordEquals(aIID, (nsXPCOMCycleCollectionParticipant ::COMTypeInfo<nsXPCOMCycleCollectionParticipant, void>:: kIID)) || LowWordEquals(aIID, (nsCycleCollectionISupports::COMTypeInfo <nsCycleCollectionISupports, void>::kIID)))) { if (LowWordEquals (aIID, (nsXPCOMCycleCollectionParticipant::COMTypeInfo<nsXPCOMCycleCollectionParticipant , void>::kIID))) { *aInstancePtr = DominatorTree::cycleCollection ::GetParticipant(); return NS_OK; } if (LowWordEquals(aIID, ( nsCycleCollectionISupports::COMTypeInfo<nsCycleCollectionISupports , void>::kIID))) { *aInstancePtr = DominatorTree::cycleCollection ::Upcast(this); return NS_OK; } foundInterface = nullptr; } else | |||
122 | NS_WRAPPERCACHE_INTERFACE_MAP_ENTRYif (aIID.Equals((nsWrapperCache::COMTypeInfo<nsWrapperCache , void>::kIID))) { *aInstancePtr = static_cast<nsWrapperCache *>(this); return NS_OK; } else | |||
123 | NS_INTERFACE_MAP_ENTRY(nsISupports)if (aIID.Equals(mozilla::detail::kImplementedIID<std::remove_reference_t <decltype(*this)>, nsISupports>)) foundInterface = static_cast <nsISupports*>(this); else | |||
124 | NS_INTERFACE_MAP_ENDfoundInterface = 0; nsresult status; if (!foundInterface) { do { static_assert( mozilla::detail::AssertionConditionType< decltype(!aIID.Equals((nsISupports::COMTypeInfo<nsISupports , void>::kIID)))>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(!aIID.Equals((nsISupports::COMTypeInfo <nsISupports, void>::kIID))))), 0))) { do { } while (false ); MOZ_ReportAssertionFailure("!aIID.Equals((nsISupports::COMTypeInfo<nsISupports, void>::kIID))" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/DominatorTree.cpp" , 124); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!aIID.Equals((nsISupports::COMTypeInfo<nsISupports, void>::kIID))" ")"); do { *((volatile int*)__null) = 124; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); status = NS_NOINTERFACE ; } else { (foundInterface)->AddRef(); status = NS_OK; } * aInstancePtr = foundInterface; return status; } | |||
125 | ||||
126 | /* virtual */ | |||
127 | JSObject* DominatorTree::WrapObject(JSContext* aCx, | |||
128 | JS::Handle<JSObject*> aGivenProto) { | |||
129 | return dom::DominatorTree_Binding::Wrap(aCx, this, aGivenProto); | |||
130 | } | |||
131 | ||||
132 | } // namespace devtools | |||
133 | } // namespace mozilla |
1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
2 | * vim: set ts=8 sts=2 et sw=2 tw=80: |
3 | * This Source Code Form is subject to the terms of the Mozilla Public |
4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | |
7 | #ifndef js_UbiNodeDominatorTree_h |
8 | #define js_UbiNodeDominatorTree_h |
9 | |
10 | #include "mozilla/Attributes.h" |
11 | #include "mozilla/DebugOnly.h" |
12 | #include "mozilla/Maybe.h" |
13 | #include "mozilla/UniquePtr.h" |
14 | |
15 | #include <utility> |
16 | |
17 | #include "js/AllocPolicy.h" |
18 | #include "js/UbiNode.h" |
19 | #include "js/UbiNodePostOrder.h" |
20 | #include "js/Utility.h" |
21 | #include "js/Vector.h" |
22 | |
23 | namespace JS { |
24 | namespace ubi { |
25 | |
26 | /** |
27 | * In a directed graph with a root node `R`, a node `A` is said to "dominate" a |
28 | * node `B` iff every path from `R` to `B` contains `A`. A node `A` is said to |
29 | * be the "immediate dominator" of a node `B` iff it dominates `B`, is not `B` |
30 | * itself, and does not dominate any other nodes which also dominate `B` in |
31 | * turn. |
32 | * |
33 | * If we take every node from a graph `G` and create a new graph `T` with edges |
34 | * to each node from its immediate dominator, then `T` is a tree (each node has |
35 | * only one immediate dominator, or none if it is the root). This tree is called |
36 | * a "dominator tree". |
37 | * |
38 | * This class represents a dominator tree constructed from a `JS::ubi::Node` |
39 | * heap graph. The domination relationship and dominator trees are useful tools |
40 | * for analyzing heap graphs because they tell you: |
41 | * |
42 | * - Exactly what could be reclaimed by the GC if some node `A` became |
43 | * unreachable: those nodes which are dominated by `A`, |
44 | * |
45 | * - The "retained size" of a node in the heap graph, in contrast to its |
46 | * "shallow size". The "shallow size" is the space taken by a node itself, |
47 | * not counting anything it references. The "retained size" of a node is its |
48 | * shallow size plus the size of all the things that would be collected if |
49 | * the original node wasn't (directly or indirectly) referencing them. In |
50 | * other words, the retained size is the shallow size of a node plus the |
51 | * shallow sizes of every other node it dominates. For example, the root |
52 | * node in a binary tree might have a small shallow size that does not take |
53 | * up much space itself, but it dominates the rest of the binary tree and |
54 | * its retained size is therefore significant (assuming no external |
55 | * references into the tree). |
56 | * |
57 | * The simple, engineered algorithm presented in "A Simple, Fast Dominance |
58 | * Algorithm" by Cooper el al[0] is used to find dominators and construct the |
59 | * dominator tree. This algorithm runs in O(n^2) time, but is faster in practice |
60 | * than alternative algorithms with better theoretical running times, such as |
61 | * Lengauer-Tarjan which runs in O(e * log(n)). The big caveat to that statement |
62 | * is that Cooper et al found it is faster in practice *on control flow graphs* |
63 | * and I'm not convinced that this property also holds on *heap* graphs. That |
64 | * said, the implementation of this algorithm is *much* simpler than |
65 | * Lengauer-Tarjan and has been found to be fast enough at least for the time |
66 | * being. |
67 | * |
68 | * [0]: http://www.cs.rice.edu/~keith/EMBED/dom.pdf |
69 | */ |
70 | class JS_PUBLIC_API DominatorTree { |
71 | private: |
72 | // Types. |
73 | |
74 | using PredecessorSets = js::HashMap<Node, NodeSetPtr, js::DefaultHasher<Node>, |
75 | js::SystemAllocPolicy>; |
76 | using NodeToIndexMap = js::HashMap<Node, uint32_t, js::DefaultHasher<Node>, |
77 | js::SystemAllocPolicy>; |
78 | class DominatedSets; |
79 | |
80 | public: |
81 | class DominatedSetRange; |
82 | |
83 | /** |
84 | * A pointer to an immediately dominated node. |
85 | * |
86 | * Don't use this type directly; it is no safer than regular pointers. This |
87 | * is only for use indirectly with range-based for loops and |
88 | * `DominatedSetRange`. |
89 | * |
90 | * @see JS::ubi::DominatorTree::getDominatedSet |
91 | */ |
92 | class DominatedNodePtr { |
93 | friend class DominatedSetRange; |
94 | |
95 | const JS::ubi::Vector<Node>& postOrder; |
96 | const uint32_t* ptr; |
97 | |
98 | DominatedNodePtr(const JS::ubi::Vector<Node>& postOrder, |
99 | const uint32_t* ptr) |
100 | : postOrder(postOrder), ptr(ptr) {} |
101 | |
102 | public: |
103 | bool operator!=(const DominatedNodePtr& rhs) const { |
104 | return ptr != rhs.ptr; |
105 | } |
106 | void operator++() { ptr++; } |
107 | const Node& operator*() const { return postOrder[*ptr]; } |
108 | }; |
109 | |
110 | /** |
111 | * A range of immediately dominated `JS::ubi::Node`s for use with |
112 | * range-based for loops. |
113 | * |
114 | * @see JS::ubi::DominatorTree::getDominatedSet |
115 | */ |
116 | class DominatedSetRange { |
117 | friend class DominatedSets; |
118 | |
119 | const JS::ubi::Vector<Node>& postOrder; |
120 | const uint32_t* beginPtr; |
121 | const uint32_t* endPtr; |
122 | |
123 | DominatedSetRange(JS::ubi::Vector<Node>& postOrder, const uint32_t* begin, |
124 | const uint32_t* end) |
125 | : postOrder(postOrder), beginPtr(begin), endPtr(end) { |
126 | MOZ_ASSERT(begin <= end)do { static_assert( mozilla::detail::AssertionConditionType< decltype(begin <= end)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(begin <= end))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("begin <= end" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 126); AnnotateMozCrashReason("MOZ_ASSERT" "(" "begin <= end" ")"); do { *((volatile int*)__null) = 126; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
127 | } |
128 | |
129 | public: |
130 | DominatedNodePtr begin() const { |
131 | MOZ_ASSERT(beginPtr <= endPtr)do { static_assert( mozilla::detail::AssertionConditionType< decltype(beginPtr <= endPtr)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(beginPtr <= endPtr))), 0) )) { do { } while (false); MOZ_ReportAssertionFailure("beginPtr <= endPtr" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 131); AnnotateMozCrashReason("MOZ_ASSERT" "(" "beginPtr <= endPtr" ")"); do { *((volatile int*)__null) = 131; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
132 | return DominatedNodePtr(postOrder, beginPtr); |
133 | } |
134 | |
135 | DominatedNodePtr end() const { return DominatedNodePtr(postOrder, endPtr); } |
136 | |
137 | size_t length() const { |
138 | MOZ_ASSERT(beginPtr <= endPtr)do { static_assert( mozilla::detail::AssertionConditionType< decltype(beginPtr <= endPtr)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(beginPtr <= endPtr))), 0) )) { do { } while (false); MOZ_ReportAssertionFailure("beginPtr <= endPtr" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 138); AnnotateMozCrashReason("MOZ_ASSERT" "(" "beginPtr <= endPtr" ")"); do { *((volatile int*)__null) = 138; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
139 | return endPtr - beginPtr; |
140 | } |
141 | |
142 | /** |
143 | * Safely skip ahead `n` dominators in the range, in O(1) time. |
144 | * |
145 | * Example usage: |
146 | * |
147 | * mozilla::Maybe<DominatedSetRange> range = |
148 | * myDominatorTree.getDominatedSet(myNode); |
149 | * if (range.isNothing()) { |
150 | * // Handle unknown nodes however you see fit... |
151 | * return false; |
152 | * } |
153 | * |
154 | * // Don't care about the first ten, for whatever reason. |
155 | * range->skip(10); |
156 | * for (const JS::ubi::Node& dominatedNode : *range) { |
157 | * // ... |
158 | * } |
159 | */ |
160 | void skip(size_t n) { |
161 | beginPtr += n; |
162 | if (beginPtr > endPtr) { |
163 | beginPtr = endPtr; |
164 | } |
165 | } |
166 | }; |
167 | |
168 | private: |
169 | /** |
170 | * The set of all dominated sets in a dominator tree. |
171 | * |
172 | * Internally stores the sets in a contiguous array, with a side table of |
173 | * indices into that contiguous array to denote the start index of each |
174 | * individual set. |
175 | */ |
176 | class DominatedSets { |
177 | JS::ubi::Vector<uint32_t> dominated; |
178 | JS::ubi::Vector<uint32_t> indices; |
179 | |
180 | DominatedSets(JS::ubi::Vector<uint32_t>&& dominated, |
181 | JS::ubi::Vector<uint32_t>&& indices) |
182 | : dominated(std::move(dominated)), indices(std::move(indices)) {} |
183 | |
184 | public: |
185 | // DominatedSets is not copy-able. |
186 | DominatedSets(const DominatedSets& rhs) = delete; |
187 | DominatedSets& operator=(const DominatedSets& rhs) = delete; |
188 | |
189 | // DominatedSets is move-able. |
190 | DominatedSets(DominatedSets&& rhs) |
191 | : dominated(std::move(rhs.dominated)), indices(std::move(rhs.indices)) { |
192 | MOZ_ASSERT(this != &rhs, "self-move not allowed")do { static_assert( mozilla::detail::AssertionConditionType< decltype(this != &rhs)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(this != &rhs))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("this != &rhs" " (" "self-move not allowed" ")", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 192); AnnotateMozCrashReason("MOZ_ASSERT" "(" "this != &rhs" ") (" "self-move not allowed" ")"); do { *((volatile int*)__null ) = 192; __attribute__((nomerge)) ::abort(); } while (false); } } while (false); |
193 | } |
194 | DominatedSets& operator=(DominatedSets&& rhs) { |
195 | this->~DominatedSets(); |
196 | new (this) DominatedSets(std::move(rhs)); |
197 | return *this; |
198 | } |
199 | |
200 | /** |
201 | * Create the DominatedSets given the mapping of a node index to its |
202 | * immediate dominator. Returns `Some` on success, `Nothing` on OOM |
203 | * failure. |
204 | */ |
205 | static mozilla::Maybe<DominatedSets> Create( |
206 | const JS::ubi::Vector<uint32_t>& doms) { |
207 | auto length = doms.length(); |
208 | MOZ_ASSERT(length < UINT32_MAX)do { static_assert( mozilla::detail::AssertionConditionType< decltype(length < (4294967295U))>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(length < (4294967295U)))) , 0))) { do { } while (false); MOZ_ReportAssertionFailure("length < (4294967295U)" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 208); AnnotateMozCrashReason("MOZ_ASSERT" "(" "length < (4294967295U)" ")"); do { *((volatile int*)__null) = 208; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
209 | |
210 | // Create a vector `dominated` holding a flattened set of buckets of |
211 | // immediately dominated children nodes, with a lookup table |
212 | // `indices` mapping from each node to the beginning of its bucket. |
213 | // |
214 | // This has three phases: |
215 | // |
216 | // 1. Iterate over the full set of nodes and count up the size of |
217 | // each bucket. These bucket sizes are temporarily stored in the |
218 | // `indices` vector. |
219 | // |
220 | // 2. Convert the `indices` vector to store the cumulative sum of |
221 | // the sizes of all buckets before each index, resulting in a |
222 | // mapping from node index to one past the end of that node's |
223 | // bucket. |
224 | // |
225 | // 3. Iterate over the full set of nodes again, filling in bucket |
226 | // entries from the end of the bucket's range to its |
227 | // beginning. This decrements each index as a bucket entry is |
228 | // filled in. After having filled in all of a bucket's entries, |
229 | // the index points to the start of the bucket. |
230 | |
231 | JS::ubi::Vector<uint32_t> dominated; |
232 | JS::ubi::Vector<uint32_t> indices; |
233 | if (!dominated.growBy(length) || !indices.growBy(length)) { |
234 | return mozilla::Nothing(); |
235 | } |
236 | |
237 | // 1 |
238 | memset(indices.begin(), 0, length * sizeof(uint32_t)); |
239 | for (uint32_t i = 0; i < length; i++) { |
240 | indices[doms[i]]++; |
241 | } |
242 | |
243 | // 2 |
244 | uint32_t sumOfSizes = 0; |
245 | for (uint32_t i = 0; i < length; i++) { |
246 | sumOfSizes += indices[i]; |
247 | MOZ_ASSERT(sumOfSizes <= length)do { static_assert( mozilla::detail::AssertionConditionType< decltype(sumOfSizes <= length)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(sumOfSizes <= length))), 0 ))) { do { } while (false); MOZ_ReportAssertionFailure("sumOfSizes <= length" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 247); AnnotateMozCrashReason("MOZ_ASSERT" "(" "sumOfSizes <= length" ")"); do { *((volatile int*)__null) = 247; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
248 | indices[i] = sumOfSizes; |
249 | } |
250 | |
251 | // 3 |
252 | for (uint32_t i = 0; i < length; i++) { |
253 | auto idxOfDom = doms[i]; |
254 | indices[idxOfDom]--; |
255 | dominated[indices[idxOfDom]] = i; |
256 | } |
257 | |
258 | #ifdef DEBUG1 |
259 | // Assert that our buckets are non-overlapping and don't run off the |
260 | // end of the vector. |
261 | uint32_t lastIndex = 0; |
262 | for (uint32_t i = 0; i < length; i++) { |
263 | MOZ_ASSERT(indices[i] >= lastIndex)do { static_assert( mozilla::detail::AssertionConditionType< decltype(indices[i] >= lastIndex)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(indices[i] >= lastIndex)) ), 0))) { do { } while (false); MOZ_ReportAssertionFailure("indices[i] >= lastIndex" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 263); AnnotateMozCrashReason("MOZ_ASSERT" "(" "indices[i] >= lastIndex" ")"); do { *((volatile int*)__null) = 263; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
264 | MOZ_ASSERT(indices[i] < length)do { static_assert( mozilla::detail::AssertionConditionType< decltype(indices[i] < length)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(indices[i] < length))), 0 ))) { do { } while (false); MOZ_ReportAssertionFailure("indices[i] < length" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 264); AnnotateMozCrashReason("MOZ_ASSERT" "(" "indices[i] < length" ")"); do { *((volatile int*)__null) = 264; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
265 | lastIndex = indices[i]; |
266 | } |
267 | #endif |
268 | |
269 | return mozilla::Some( |
270 | DominatedSets(std::move(dominated), std::move(indices))); |
271 | } |
272 | |
273 | /** |
274 | * Get the set of nodes immediately dominated by the node at |
275 | * `postOrder[nodeIndex]`. |
276 | */ |
277 | DominatedSetRange dominatedSet(JS::ubi::Vector<Node>& postOrder, |
278 | uint32_t nodeIndex) const { |
279 | MOZ_ASSERT(postOrder.length() == indices.length())do { static_assert( mozilla::detail::AssertionConditionType< decltype(postOrder.length() == indices.length())>::isValid , "invalid assertion condition"); if ((__builtin_expect(!!(!( !!(postOrder.length() == indices.length()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("postOrder.length() == indices.length()" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 279); AnnotateMozCrashReason("MOZ_ASSERT" "(" "postOrder.length() == indices.length()" ")"); do { *((volatile int*)__null) = 279; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
280 | MOZ_ASSERT(nodeIndex < indices.length())do { static_assert( mozilla::detail::AssertionConditionType< decltype(nodeIndex < indices.length())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(nodeIndex < indices.length ()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("nodeIndex < indices.length()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 280); AnnotateMozCrashReason("MOZ_ASSERT" "(" "nodeIndex < indices.length()" ")"); do { *((volatile int*)__null) = 280; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
281 | auto end = nodeIndex == indices.length() - 1 |
282 | ? dominated.end() |
283 | : &dominated[indices[nodeIndex + 1]]; |
284 | return DominatedSetRange(postOrder, &dominated[indices[nodeIndex]], end); |
285 | } |
286 | }; |
287 | |
288 | private: |
289 | // Data members. |
290 | JS::ubi::Vector<Node> postOrder; |
291 | NodeToIndexMap nodeToPostOrderIndex; |
292 | JS::ubi::Vector<uint32_t> doms; |
293 | DominatedSets dominatedSets; |
294 | mozilla::Maybe<JS::ubi::Vector<JS::ubi::Node::Size>> retainedSizes; |
295 | |
296 | private: |
297 | // We use `UNDEFINED` as a sentinel value in the `doms` vector to signal |
298 | // that we haven't found any dominators for the node at the corresponding |
299 | // index in `postOrder` yet. |
300 | static const uint32_t UNDEFINED = UINT32_MAX(4294967295U); |
301 | |
302 | DominatorTree(JS::ubi::Vector<Node>&& postOrder, |
303 | NodeToIndexMap&& nodeToPostOrderIndex, |
304 | JS::ubi::Vector<uint32_t>&& doms, DominatedSets&& dominatedSets) |
305 | : postOrder(std::move(postOrder)), |
306 | nodeToPostOrderIndex(std::move(nodeToPostOrderIndex)), |
307 | doms(std::move(doms)), |
308 | dominatedSets(std::move(dominatedSets)), |
309 | retainedSizes(mozilla::Nothing()) {} |
310 | |
311 | static uint32_t intersect(JS::ubi::Vector<uint32_t>& doms, uint32_t finger1, |
312 | uint32_t finger2) { |
313 | while (finger1 != finger2) { |
314 | if (finger1 < finger2) { |
315 | finger1 = doms[finger1]; |
316 | } else if (finger2 < finger1) { |
317 | finger2 = doms[finger2]; |
318 | } |
319 | } |
320 | return finger1; |
321 | } |
322 | |
323 | // Do the post order traversal of the heap graph and populate our |
324 | // predecessor sets. |
325 | [[nodiscard]] static bool doTraversal(JSContext* cx, AutoCheckCannotGC& noGC, |
326 | const Node& root, |
327 | JS::ubi::Vector<Node>& postOrder, |
328 | PredecessorSets& predecessorSets) { |
329 | uint32_t nodeCount = 0; |
330 | auto onNode = [&](const Node& node) { |
331 | nodeCount++; |
332 | if (MOZ_UNLIKELY(nodeCount == UINT32_MAX)(__builtin_expect(!!(nodeCount == (4294967295U)), 0))) { |
333 | return false; |
334 | } |
335 | return postOrder.append(node); |
336 | }; |
337 | |
338 | auto onEdge = [&](const Node& origin, const Edge& edge) { |
339 | auto p = predecessorSets.lookupForAdd(edge.referent); |
340 | if (!p) { |
341 | mozilla::UniquePtr<NodeSet, DeletePolicy<NodeSet>> set( |
342 | js_new<NodeSet>()); |
343 | if (!set || !predecessorSets.add(p, edge.referent, std::move(set))) { |
344 | return false; |
345 | } |
346 | } |
347 | MOZ_ASSERT(p && p->value())do { static_assert( mozilla::detail::AssertionConditionType< decltype(p && p->value())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(p && p->value())) ), 0))) { do { } while (false); MOZ_ReportAssertionFailure("p && p->value()" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 347); AnnotateMozCrashReason("MOZ_ASSERT" "(" "p && p->value()" ")"); do { *((volatile int*)__null) = 347; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
348 | return p->value()->put(origin); |
349 | }; |
350 | |
351 | PostOrder traversal(cx, noGC); |
352 | return traversal.addStart(root) && traversal.traverse(onNode, onEdge); |
353 | } |
354 | |
355 | // Populates the given `map` with an entry for each node to its index in |
356 | // `postOrder`. |
357 | [[nodiscard]] static bool mapNodesToTheirIndices( |
358 | JS::ubi::Vector<Node>& postOrder, NodeToIndexMap& map) { |
359 | MOZ_ASSERT(map.empty())do { static_assert( mozilla::detail::AssertionConditionType< decltype(map.empty())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(map.empty()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("map.empty()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 359); AnnotateMozCrashReason("MOZ_ASSERT" "(" "map.empty()" ")"); do { *((volatile int*)__null) = 359; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
360 | MOZ_ASSERT(postOrder.length() < UINT32_MAX)do { static_assert( mozilla::detail::AssertionConditionType< decltype(postOrder.length() < (4294967295U))>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(! !(postOrder.length() < (4294967295U)))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("postOrder.length() < (4294967295U)" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 360); AnnotateMozCrashReason("MOZ_ASSERT" "(" "postOrder.length() < (4294967295U)" ")"); do { *((volatile int*)__null) = 360; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
361 | uint32_t length = postOrder.length(); |
362 | if (!map.reserve(length)) { |
363 | return false; |
364 | } |
365 | for (uint32_t i = 0; i < length; i++) { |
366 | map.putNewInfallible(postOrder[i], i); |
367 | } |
368 | return true; |
369 | } |
370 | |
371 | // Convert the Node -> NodeSet predecessorSets to a index -> Vector<index> |
372 | // form. |
373 | [[nodiscard]] static bool convertPredecessorSetsToVectors( |
374 | const Node& root, JS::ubi::Vector<Node>& postOrder, |
375 | PredecessorSets& predecessorSets, NodeToIndexMap& nodeToPostOrderIndex, |
376 | JS::ubi::Vector<JS::ubi::Vector<uint32_t>>& predecessorVectors) { |
377 | MOZ_ASSERT(postOrder.length() < UINT32_MAX)do { static_assert( mozilla::detail::AssertionConditionType< decltype(postOrder.length() < (4294967295U))>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(! !(postOrder.length() < (4294967295U)))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("postOrder.length() < (4294967295U)" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 377); AnnotateMozCrashReason("MOZ_ASSERT" "(" "postOrder.length() < (4294967295U)" ")"); do { *((volatile int*)__null) = 377; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
378 | uint32_t length = postOrder.length(); |
379 | |
380 | MOZ_ASSERT(predecessorVectors.length() == 0)do { static_assert( mozilla::detail::AssertionConditionType< decltype(predecessorVectors.length() == 0)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(predecessorVectors.length() == 0))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("predecessorVectors.length() == 0", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 380); AnnotateMozCrashReason("MOZ_ASSERT" "(" "predecessorVectors.length() == 0" ")"); do { *((volatile int*)__null) = 380; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
381 | if (!predecessorVectors.growBy(length)) { |
382 | return false; |
383 | } |
384 | |
385 | for (uint32_t i = 0; i < length - 1; i++) { |
386 | auto& node = postOrder[i]; |
387 | MOZ_ASSERT(node != root,do { static_assert( mozilla::detail::AssertionConditionType< decltype(node != root)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(node != root))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("node != root" " (" "Only the last node should be root, since this was a post " "order traversal." ")", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 389); AnnotateMozCrashReason("MOZ_ASSERT" "(" "node != root" ") (" "Only the last node should be root, since this was a post " "order traversal." ")"); do { *((volatile int*)__null) = 389 ; __attribute__((nomerge)) ::abort(); } while (false); } } while (false) |
388 | "Only the last node should be root, since this was a post "do { static_assert( mozilla::detail::AssertionConditionType< decltype(node != root)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(node != root))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("node != root" " (" "Only the last node should be root, since this was a post " "order traversal." ")", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 389); AnnotateMozCrashReason("MOZ_ASSERT" "(" "node != root" ") (" "Only the last node should be root, since this was a post " "order traversal." ")"); do { *((volatile int*)__null) = 389 ; __attribute__((nomerge)) ::abort(); } while (false); } } while (false) |
389 | "order traversal.")do { static_assert( mozilla::detail::AssertionConditionType< decltype(node != root)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(node != root))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("node != root" " (" "Only the last node should be root, since this was a post " "order traversal." ")", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 389); AnnotateMozCrashReason("MOZ_ASSERT" "(" "node != root" ") (" "Only the last node should be root, since this was a post " "order traversal." ")"); do { *((volatile int*)__null) = 389 ; __attribute__((nomerge)) ::abort(); } while (false); } } while (false); |
390 | |
391 | auto ptr = predecessorSets.lookup(node); |
392 | MOZ_ASSERT(ptr,do { static_assert( mozilla::detail::AssertionConditionType< decltype(ptr)>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(!!(ptr))), 0))) { do { } while (false ); MOZ_ReportAssertionFailure("ptr" " (" "Because this isn't the root, it had better have " "predecessors, or else how " "did we even find it." ")", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 395); AnnotateMozCrashReason("MOZ_ASSERT" "(" "ptr" ") (" "Because this isn't the root, it had better have " "predecessors, or else how " "did we even find it." ")"); do { *((volatile int*)__null) = 395; __attribute__((nomerge)) :: abort(); } while (false); } } while (false) |
393 | "Because this isn't the root, it had better have "do { static_assert( mozilla::detail::AssertionConditionType< decltype(ptr)>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(!!(ptr))), 0))) { do { } while (false ); MOZ_ReportAssertionFailure("ptr" " (" "Because this isn't the root, it had better have " "predecessors, or else how " "did we even find it." ")", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 395); AnnotateMozCrashReason("MOZ_ASSERT" "(" "ptr" ") (" "Because this isn't the root, it had better have " "predecessors, or else how " "did we even find it." ")"); do { *((volatile int*)__null) = 395; __attribute__((nomerge)) :: abort(); } while (false); } } while (false) |
394 | "predecessors, or else how "do { static_assert( mozilla::detail::AssertionConditionType< decltype(ptr)>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(!!(ptr))), 0))) { do { } while (false ); MOZ_ReportAssertionFailure("ptr" " (" "Because this isn't the root, it had better have " "predecessors, or else how " "did we even find it." ")", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 395); AnnotateMozCrashReason("MOZ_ASSERT" "(" "ptr" ") (" "Because this isn't the root, it had better have " "predecessors, or else how " "did we even find it." ")"); do { *((volatile int*)__null) = 395; __attribute__((nomerge)) :: abort(); } while (false); } } while (false) |
395 | "did we even find it.")do { static_assert( mozilla::detail::AssertionConditionType< decltype(ptr)>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(!!(ptr))), 0))) { do { } while (false ); MOZ_ReportAssertionFailure("ptr" " (" "Because this isn't the root, it had better have " "predecessors, or else how " "did we even find it." ")", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 395); AnnotateMozCrashReason("MOZ_ASSERT" "(" "ptr" ") (" "Because this isn't the root, it had better have " "predecessors, or else how " "did we even find it." ")"); do { *((volatile int*)__null) = 395; __attribute__((nomerge)) :: abort(); } while (false); } } while (false); |
396 | |
397 | auto& predecessors = ptr->value(); |
398 | if (!predecessorVectors[i].reserve(predecessors->count())) { |
399 | return false; |
400 | } |
401 | for (auto range = predecessors->all(); !range.empty(); range.popFront()) { |
402 | auto ptr = nodeToPostOrderIndex.lookup(range.front()); |
403 | MOZ_ASSERT(ptr)do { static_assert( mozilla::detail::AssertionConditionType< decltype(ptr)>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(!!(ptr))), 0))) { do { } while (false ); MOZ_ReportAssertionFailure("ptr", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 403); AnnotateMozCrashReason("MOZ_ASSERT" "(" "ptr" ")"); do { *((volatile int*)__null) = 403; __attribute__((nomerge)) :: abort(); } while (false); } } while (false); |
404 | predecessorVectors[i].infallibleAppend(ptr->value()); |
405 | } |
406 | } |
407 | predecessorSets.clearAndCompact(); |
408 | return true; |
409 | } |
410 | |
411 | // Initialize `doms` such that the immediate dominator of the `root` is the |
412 | // `root` itself and all others are `UNDEFINED`. |
413 | [[nodiscard]] static bool initializeDominators( |
414 | JS::ubi::Vector<uint32_t>& doms, uint32_t length) { |
415 | MOZ_ASSERT(doms.length() == 0)do { static_assert( mozilla::detail::AssertionConditionType< decltype(doms.length() == 0)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(doms.length() == 0))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("doms.length() == 0" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 415); AnnotateMozCrashReason("MOZ_ASSERT" "(" "doms.length() == 0" ")"); do { *((volatile int*)__null) = 415; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
416 | if (!doms.growByUninitialized(length)) { |
417 | return false; |
418 | } |
419 | doms[length - 1] = length - 1; |
420 | for (uint32_t i = 0; i < length - 1; i++) { |
421 | doms[i] = UNDEFINED; |
422 | } |
423 | return true; |
424 | } |
425 | |
426 | void assertSanity() const { |
427 | MOZ_ASSERT(postOrder.length() == doms.length())do { static_assert( mozilla::detail::AssertionConditionType< decltype(postOrder.length() == doms.length())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(postOrder.length() == doms.length ()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("postOrder.length() == doms.length()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 427); AnnotateMozCrashReason("MOZ_ASSERT" "(" "postOrder.length() == doms.length()" ")"); do { *((volatile int*)__null) = 427; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
428 | MOZ_ASSERT(postOrder.length() == nodeToPostOrderIndex.count())do { static_assert( mozilla::detail::AssertionConditionType< decltype(postOrder.length() == nodeToPostOrderIndex.count())> ::isValid, "invalid assertion condition"); if ((__builtin_expect (!!(!(!!(postOrder.length() == nodeToPostOrderIndex.count())) ), 0))) { do { } while (false); MOZ_ReportAssertionFailure("postOrder.length() == nodeToPostOrderIndex.count()" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 428); AnnotateMozCrashReason("MOZ_ASSERT" "(" "postOrder.length() == nodeToPostOrderIndex.count()" ")"); do { *((volatile int*)__null) = 428; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
429 | MOZ_ASSERT_IF(retainedSizes.isSome(),do { if (retainedSizes.isSome()) { do { static_assert( mozilla ::detail::AssertionConditionType<decltype(postOrder.length () == retainedSizes->length())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(postOrder.length() == retainedSizes ->length()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("postOrder.length() == retainedSizes->length()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 430); AnnotateMozCrashReason("MOZ_ASSERT" "(" "postOrder.length() == retainedSizes->length()" ")"); do { *((volatile int*)__null) = 430; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); } } while ( false) |
430 | postOrder.length() == retainedSizes->length())do { if (retainedSizes.isSome()) { do { static_assert( mozilla ::detail::AssertionConditionType<decltype(postOrder.length () == retainedSizes->length())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(postOrder.length() == retainedSizes ->length()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("postOrder.length() == retainedSizes->length()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 430); AnnotateMozCrashReason("MOZ_ASSERT" "(" "postOrder.length() == retainedSizes->length()" ")"); do { *((volatile int*)__null) = 430; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); } } while ( false); |
431 | } |
432 | |
433 | [[nodiscard]] bool computeRetainedSizes(mozilla::MallocSizeOf mallocSizeOf) { |
434 | MOZ_ASSERT(retainedSizes.isNothing())do { static_assert( mozilla::detail::AssertionConditionType< decltype(retainedSizes.isNothing())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(retainedSizes.isNothing()))) , 0))) { do { } while (false); MOZ_ReportAssertionFailure("retainedSizes.isNothing()" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 434); AnnotateMozCrashReason("MOZ_ASSERT" "(" "retainedSizes.isNothing()" ")"); do { *((volatile int*)__null) = 434; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
435 | auto length = postOrder.length(); |
436 | |
437 | retainedSizes.emplace(); |
438 | if (!retainedSizes->growBy(length)) { |
439 | retainedSizes = mozilla::Nothing(); |
440 | return false; |
441 | } |
442 | |
443 | // Iterate in forward order so that we know all of a node's children in |
444 | // the dominator tree have already had their retained size |
445 | // computed. Then we can simply say that the retained size of a node is |
446 | // its shallow size (JS::ubi::Node::size) plus the retained sizes of its |
447 | // immediate children in the tree. |
448 | |
449 | for (uint32_t i = 0; i < length; i++) { |
450 | auto size = postOrder[i].size(mallocSizeOf); |
451 | |
452 | for (const auto& dominated : dominatedSets.dominatedSet(postOrder, i)) { |
453 | // The root node dominates itself, but shouldn't contribute to |
454 | // its own retained size. |
455 | if (dominated == postOrder[length - 1]) { |
456 | MOZ_ASSERT(i == length - 1)do { static_assert( mozilla::detail::AssertionConditionType< decltype(i == length - 1)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(i == length - 1))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("i == length - 1" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 456); AnnotateMozCrashReason("MOZ_ASSERT" "(" "i == length - 1" ")"); do { *((volatile int*)__null) = 456; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
457 | continue; |
458 | } |
459 | |
460 | auto ptr = nodeToPostOrderIndex.lookup(dominated); |
461 | MOZ_ASSERT(ptr)do { static_assert( mozilla::detail::AssertionConditionType< decltype(ptr)>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(!!(ptr))), 0))) { do { } while (false ); MOZ_ReportAssertionFailure("ptr", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 461); AnnotateMozCrashReason("MOZ_ASSERT" "(" "ptr" ")"); do { *((volatile int*)__null) = 461; __attribute__((nomerge)) :: abort(); } while (false); } } while (false); |
462 | auto idxOfDominated = ptr->value(); |
463 | MOZ_ASSERT(idxOfDominated < i)do { static_assert( mozilla::detail::AssertionConditionType< decltype(idxOfDominated < i)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(idxOfDominated < i))), 0) )) { do { } while (false); MOZ_ReportAssertionFailure("idxOfDominated < i" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 463); AnnotateMozCrashReason("MOZ_ASSERT" "(" "idxOfDominated < i" ")"); do { *((volatile int*)__null) = 463; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
464 | size += retainedSizes.ref()[idxOfDominated]; |
465 | } |
466 | |
467 | retainedSizes.ref()[i] = size; |
468 | } |
469 | |
470 | return true; |
471 | } |
472 | |
473 | public: |
474 | // DominatorTree is not copy-able. |
475 | DominatorTree(const DominatorTree&) = delete; |
476 | DominatorTree& operator=(const DominatorTree&) = delete; |
477 | |
478 | // DominatorTree is move-able. |
479 | DominatorTree(DominatorTree&& rhs) |
480 | : postOrder(std::move(rhs.postOrder)), |
481 | nodeToPostOrderIndex(std::move(rhs.nodeToPostOrderIndex)), |
482 | doms(std::move(rhs.doms)), |
483 | dominatedSets(std::move(rhs.dominatedSets)), |
484 | retainedSizes(std::move(rhs.retainedSizes)) { |
485 | MOZ_ASSERT(this != &rhs, "self-move is not allowed")do { static_assert( mozilla::detail::AssertionConditionType< decltype(this != &rhs)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(this != &rhs))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("this != &rhs" " (" "self-move is not allowed" ")", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 485); AnnotateMozCrashReason("MOZ_ASSERT" "(" "this != &rhs" ") (" "self-move is not allowed" ")"); do { *((volatile int* )__null) = 485; __attribute__((nomerge)) ::abort(); } while ( false); } } while (false); |
486 | } |
487 | DominatorTree& operator=(DominatorTree&& rhs) { |
488 | this->~DominatorTree(); |
489 | new (this) DominatorTree(std::move(rhs)); |
490 | return *this; |
491 | } |
492 | |
493 | /** |
494 | * Construct a `DominatorTree` of the heap graph visible from `root`. The |
495 | * `root` is also used as the root of the resulting dominator tree. |
496 | * |
497 | * The resulting `DominatorTree` instance must not outlive the |
498 | * `JS::ubi::Node` graph it was constructed from. |
499 | * |
500 | * - For `JS::ubi::Node` graphs backed by the live heap graph, this means |
501 | * that the `DominatorTree`'s lifetime _must_ be contained within the |
502 | * scope of the provided `AutoCheckCannotGC` reference because a GC will |
503 | * invalidate the nodes. |
504 | * |
505 | * - For `JS::ubi::Node` graphs backed by some other offline structure |
506 | * provided by the embedder, the resulting `DominatorTree`'s lifetime is |
507 | * bounded by that offline structure's lifetime. |
508 | * |
509 | * In practice, this means that within SpiderMonkey we must treat |
510 | * `DominatorTree` as if it were backed by the live heap graph and trust |
511 | * that embedders with knowledge of the graph's implementation will do the |
512 | * Right Thing. |
513 | * |
514 | * Returns `mozilla::Nothing()` on OOM failure. It is the caller's |
515 | * responsibility to handle and report the OOM. |
516 | */ |
517 | static mozilla::Maybe<DominatorTree> Create(JSContext* cx, |
518 | AutoCheckCannotGC& noGC, |
519 | const Node& root) { |
520 | JS::ubi::Vector<Node> postOrder; |
521 | PredecessorSets predecessorSets; |
522 | if (!doTraversal(cx, noGC, root, postOrder, predecessorSets)) { |
523 | return mozilla::Nothing(); |
524 | } |
525 | |
526 | MOZ_ASSERT(postOrder.length() < UINT32_MAX)do { static_assert( mozilla::detail::AssertionConditionType< decltype(postOrder.length() < (4294967295U))>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(! !(postOrder.length() < (4294967295U)))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("postOrder.length() < (4294967295U)" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 526); AnnotateMozCrashReason("MOZ_ASSERT" "(" "postOrder.length() < (4294967295U)" ")"); do { *((volatile int*)__null) = 526; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
527 | uint32_t length = postOrder.length(); |
528 | MOZ_ASSERT(postOrder[length - 1] == root)do { static_assert( mozilla::detail::AssertionConditionType< decltype(postOrder[length - 1] == root)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(postOrder[length - 1] == root ))), 0))) { do { } while (false); MOZ_ReportAssertionFailure( "postOrder[length - 1] == root", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 528); AnnotateMozCrashReason("MOZ_ASSERT" "(" "postOrder[length - 1] == root" ")"); do { *((volatile int*)__null) = 528; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
529 | |
530 | // From here on out we wish to avoid hash table lookups, and we use |
531 | // indices into `postOrder` instead of actual nodes wherever |
532 | // possible. This greatly improves the performance of this |
533 | // implementation, but we have to pay a little bit of upfront cost to |
534 | // convert our data structures to play along first. |
535 | |
536 | NodeToIndexMap nodeToPostOrderIndex(postOrder.length()); |
537 | if (!mapNodesToTheirIndices(postOrder, nodeToPostOrderIndex)) { |
538 | return mozilla::Nothing(); |
539 | } |
540 | |
541 | JS::ubi::Vector<JS::ubi::Vector<uint32_t>> predecessorVectors; |
542 | if (!convertPredecessorSetsToVectors(root, postOrder, predecessorSets, |
543 | nodeToPostOrderIndex, |
544 | predecessorVectors)) |
545 | return mozilla::Nothing(); |
546 | |
547 | JS::ubi::Vector<uint32_t> doms; |
548 | if (!initializeDominators(doms, length)) { |
549 | return mozilla::Nothing(); |
550 | } |
551 | |
552 | bool changed = true; |
553 | while (changed) { |
554 | changed = false; |
555 | |
556 | // Iterate over the non-root nodes in reverse post order. |
557 | for (uint32_t indexPlusOne = length - 1; indexPlusOne > 0; |
558 | indexPlusOne--) { |
559 | MOZ_ASSERT(postOrder[indexPlusOne - 1] != root)do { static_assert( mozilla::detail::AssertionConditionType< decltype(postOrder[indexPlusOne - 1] != root)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(postOrder[indexPlusOne - 1] != root))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("postOrder[indexPlusOne - 1] != root", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 559); AnnotateMozCrashReason("MOZ_ASSERT" "(" "postOrder[indexPlusOne - 1] != root" ")"); do { *((volatile int*)__null) = 559; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
560 | |
561 | // Take the intersection of every predecessor's dominator set; |
562 | // that is the current best guess at the immediate dominator for |
563 | // this node. |
564 | |
565 | uint32_t newIDomIdx = UNDEFINED; |
566 | |
567 | auto& predecessors = predecessorVectors[indexPlusOne - 1]; |
568 | auto range = predecessors.all(); |
569 | for (; !range.empty(); range.popFront()) { |
570 | auto idx = range.front(); |
571 | if (doms[idx] != UNDEFINED) { |
572 | newIDomIdx = idx; |
573 | break; |
574 | } |
575 | } |
576 | |
577 | MOZ_ASSERT(newIDomIdx != UNDEFINED,do { static_assert( mozilla::detail::AssertionConditionType< decltype(newIDomIdx != UNDEFINED)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(newIDomIdx != UNDEFINED))), 0 ))) { do { } while (false); MOZ_ReportAssertionFailure("newIDomIdx != UNDEFINED" " (" "Because the root is initialized to dominate itself and is " "the first " "node in every path, there must exist a predecessor to this " "node that " "also has a dominator." ")", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 582); AnnotateMozCrashReason("MOZ_ASSERT" "(" "newIDomIdx != UNDEFINED" ") (" "Because the root is initialized to dominate itself and is " "the first " "node in every path, there must exist a predecessor to this " "node that " "also has a dominator." ")"); do { *((volatile int *)__null) = 582; __attribute__((nomerge)) ::abort(); } while ( false); } } while (false) |
578 | "Because the root is initialized to dominate itself and is "do { static_assert( mozilla::detail::AssertionConditionType< decltype(newIDomIdx != UNDEFINED)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(newIDomIdx != UNDEFINED))), 0 ))) { do { } while (false); MOZ_ReportAssertionFailure("newIDomIdx != UNDEFINED" " (" "Because the root is initialized to dominate itself and is " "the first " "node in every path, there must exist a predecessor to this " "node that " "also has a dominator." ")", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 582); AnnotateMozCrashReason("MOZ_ASSERT" "(" "newIDomIdx != UNDEFINED" ") (" "Because the root is initialized to dominate itself and is " "the first " "node in every path, there must exist a predecessor to this " "node that " "also has a dominator." ")"); do { *((volatile int *)__null) = 582; __attribute__((nomerge)) ::abort(); } while ( false); } } while (false) |
579 | "the first "do { static_assert( mozilla::detail::AssertionConditionType< decltype(newIDomIdx != UNDEFINED)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(newIDomIdx != UNDEFINED))), 0 ))) { do { } while (false); MOZ_ReportAssertionFailure("newIDomIdx != UNDEFINED" " (" "Because the root is initialized to dominate itself and is " "the first " "node in every path, there must exist a predecessor to this " "node that " "also has a dominator." ")", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 582); AnnotateMozCrashReason("MOZ_ASSERT" "(" "newIDomIdx != UNDEFINED" ") (" "Because the root is initialized to dominate itself and is " "the first " "node in every path, there must exist a predecessor to this " "node that " "also has a dominator." ")"); do { *((volatile int *)__null) = 582; __attribute__((nomerge)) ::abort(); } while ( false); } } while (false) |
580 | "node in every path, there must exist a predecessor to this "do { static_assert( mozilla::detail::AssertionConditionType< decltype(newIDomIdx != UNDEFINED)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(newIDomIdx != UNDEFINED))), 0 ))) { do { } while (false); MOZ_ReportAssertionFailure("newIDomIdx != UNDEFINED" " (" "Because the root is initialized to dominate itself and is " "the first " "node in every path, there must exist a predecessor to this " "node that " "also has a dominator." ")", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 582); AnnotateMozCrashReason("MOZ_ASSERT" "(" "newIDomIdx != UNDEFINED" ") (" "Because the root is initialized to dominate itself and is " "the first " "node in every path, there must exist a predecessor to this " "node that " "also has a dominator." ")"); do { *((volatile int *)__null) = 582; __attribute__((nomerge)) ::abort(); } while ( false); } } while (false) |
581 | "node that "do { static_assert( mozilla::detail::AssertionConditionType< decltype(newIDomIdx != UNDEFINED)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(newIDomIdx != UNDEFINED))), 0 ))) { do { } while (false); MOZ_ReportAssertionFailure("newIDomIdx != UNDEFINED" " (" "Because the root is initialized to dominate itself and is " "the first " "node in every path, there must exist a predecessor to this " "node that " "also has a dominator." ")", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 582); AnnotateMozCrashReason("MOZ_ASSERT" "(" "newIDomIdx != UNDEFINED" ") (" "Because the root is initialized to dominate itself and is " "the first " "node in every path, there must exist a predecessor to this " "node that " "also has a dominator." ")"); do { *((volatile int *)__null) = 582; __attribute__((nomerge)) ::abort(); } while ( false); } } while (false) |
582 | "also has a dominator.")do { static_assert( mozilla::detail::AssertionConditionType< decltype(newIDomIdx != UNDEFINED)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(newIDomIdx != UNDEFINED))), 0 ))) { do { } while (false); MOZ_ReportAssertionFailure("newIDomIdx != UNDEFINED" " (" "Because the root is initialized to dominate itself and is " "the first " "node in every path, there must exist a predecessor to this " "node that " "also has a dominator." ")", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 582); AnnotateMozCrashReason("MOZ_ASSERT" "(" "newIDomIdx != UNDEFINED" ") (" "Because the root is initialized to dominate itself and is " "the first " "node in every path, there must exist a predecessor to this " "node that " "also has a dominator." ")"); do { *((volatile int *)__null) = 582; __attribute__((nomerge)) ::abort(); } while ( false); } } while (false); |
583 | |
584 | for (; !range.empty(); range.popFront()) { |
585 | auto idx = range.front(); |
586 | if (doms[idx] != UNDEFINED) { |
587 | newIDomIdx = intersect(doms, newIDomIdx, idx); |
588 | } |
589 | } |
590 | |
591 | // If the immediate dominator changed, we will have to do |
592 | // another pass of the outer while loop to continue the forward |
593 | // dataflow. |
594 | if (newIDomIdx != doms[indexPlusOne - 1]) { |
595 | doms[indexPlusOne - 1] = newIDomIdx; |
596 | changed = true; |
597 | } |
598 | } |
599 | } |
600 | |
601 | auto maybeDominatedSets = DominatedSets::Create(doms); |
602 | if (maybeDominatedSets.isNothing()) { |
603 | return mozilla::Nothing(); |
604 | } |
605 | |
606 | return mozilla::Some( |
607 | DominatorTree(std::move(postOrder), std::move(nodeToPostOrderIndex), |
608 | std::move(doms), std::move(*maybeDominatedSets))); |
609 | } |
610 | |
611 | /** |
612 | * Get the root node for this dominator tree. |
613 | */ |
614 | const Node& root() const { return postOrder[postOrder.length() - 1]; } |
615 | |
616 | /** |
617 | * Return the immediate dominator of the given `node`. If `node` was not |
618 | * reachable from the `root` that this dominator tree was constructed from, |
619 | * then return the null `JS::ubi::Node`. |
620 | */ |
621 | Node getImmediateDominator(const Node& node) const { |
622 | assertSanity(); |
623 | auto ptr = nodeToPostOrderIndex.lookup(node); |
624 | if (!ptr) { |
625 | return Node(); |
626 | } |
627 | |
628 | auto idx = ptr->value(); |
629 | MOZ_ASSERT(idx < postOrder.length())do { static_assert( mozilla::detail::AssertionConditionType< decltype(idx < postOrder.length())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(idx < postOrder.length()) )), 0))) { do { } while (false); MOZ_ReportAssertionFailure("idx < postOrder.length()" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 629); AnnotateMozCrashReason("MOZ_ASSERT" "(" "idx < postOrder.length()" ")"); do { *((volatile int*)__null) = 629; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
630 | return postOrder[doms[idx]]; |
631 | } |
632 | |
633 | /** |
634 | * Get the set of nodes immediately dominated by the given `node`. If `node` |
635 | * is not a member of this dominator tree, return `Nothing`. |
636 | * |
637 | * Example usage: |
638 | * |
639 | * mozilla::Maybe<DominatedSetRange> range = |
640 | * myDominatorTree.getDominatedSet(myNode); |
641 | * if (range.isNothing()) { |
642 | * // Handle unknown node however you see fit... |
643 | * return false; |
644 | * } |
645 | * |
646 | * for (const JS::ubi::Node& dominatedNode : *range) { |
647 | * // Do something with each immediately dominated node... |
648 | * } |
649 | */ |
650 | mozilla::Maybe<DominatedSetRange> getDominatedSet(const Node& node) { |
651 | assertSanity(); |
652 | auto ptr = nodeToPostOrderIndex.lookup(node); |
653 | if (!ptr) { |
654 | return mozilla::Nothing(); |
655 | } |
656 | |
657 | auto idx = ptr->value(); |
658 | MOZ_ASSERT(idx < postOrder.length())do { static_assert( mozilla::detail::AssertionConditionType< decltype(idx < postOrder.length())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(idx < postOrder.length()) )), 0))) { do { } while (false); MOZ_ReportAssertionFailure("idx < postOrder.length()" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 658); AnnotateMozCrashReason("MOZ_ASSERT" "(" "idx < postOrder.length()" ")"); do { *((volatile int*)__null) = 658; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
659 | return mozilla::Some(dominatedSets.dominatedSet(postOrder, idx)); |
660 | } |
661 | |
662 | /** |
663 | * Get the retained size of the given `node`. The size is placed in |
664 | * `outSize`, or 0 if `node` is not a member of the dominator tree. Returns |
665 | * false on OOM failure, leaving `outSize` unchanged. |
666 | */ |
667 | [[nodiscard]] bool getRetainedSize(const Node& node, |
668 | mozilla::MallocSizeOf mallocSizeOf, |
669 | Node::Size& outSize) { |
670 | assertSanity(); |
671 | auto ptr = nodeToPostOrderIndex.lookup(node); |
672 | if (!ptr) { |
673 | outSize = 0; |
674 | return true; |
675 | } |
676 | |
677 | if (retainedSizes.isNothing() && !computeRetainedSizes(mallocSizeOf)) { |
678 | return false; |
679 | } |
680 | |
681 | auto idx = ptr->value(); |
682 | MOZ_ASSERT(idx < postOrder.length())do { static_assert( mozilla::detail::AssertionConditionType< decltype(idx < postOrder.length())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(idx < postOrder.length()) )), 0))) { do { } while (false); MOZ_ReportAssertionFailure("idx < postOrder.length()" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h" , 682); AnnotateMozCrashReason("MOZ_ASSERT" "(" "idx < postOrder.length()" ")"); do { *((volatile int*)__null) = 682; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
683 | outSize = retainedSizes.ref()[idx]; |
684 | return true; |
685 | } |
686 | }; |
687 | |
688 | } // namespace ubi |
689 | } // namespace JS |
690 | |
691 | #endif // js_UbiNodeDominatorTree_h |
1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ | |||
2 | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ | |||
3 | /* This Source Code Form is subject to the terms of the Mozilla Public | |||
4 | * License, v. 2.0. If a copy of the MPL was not distributed with this | |||
5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | |||
6 | ||||
7 | //--------------------------------------------------------------------------- | |||
8 | // Overview | |||
9 | //--------------------------------------------------------------------------- | |||
10 | // | |||
11 | // This file defines HashMap<Key, Value> and HashSet<T>, hash tables that are | |||
12 | // fast and have a nice API. | |||
13 | // | |||
14 | // Both hash tables have two optional template parameters. | |||
15 | // | |||
16 | // - HashPolicy. This defines the operations for hashing and matching keys. The | |||
17 | // default HashPolicy is appropriate when both of the following two | |||
18 | // conditions are true. | |||
19 | // | |||
20 | // - The key type stored in the table (|Key| for |HashMap<Key, Value>|, |T| | |||
21 | // for |HashSet<T>|) is an integer, pointer, UniquePtr, float, or double. | |||
22 | // | |||
23 | // - The type used for lookups (|Lookup|) is the same as the key type. This | |||
24 | // is usually the case, but not always. | |||
25 | // | |||
26 | // There is also a |CStringHasher| policy for |char*| keys. If your keys | |||
27 | // don't match any of the above cases, you must provide your own hash policy; | |||
28 | // see the "Hash Policy" section below. | |||
29 | // | |||
30 | // - AllocPolicy. This defines how allocations are done by the table. | |||
31 | // | |||
32 | // - |MallocAllocPolicy| is the default and is usually appropriate; note that | |||
33 | // operations (such as insertions) that might cause allocations are | |||
34 | // fallible and must be checked for OOM. These checks are enforced by the | |||
35 | // use of [[nodiscard]]. | |||
36 | // | |||
37 | // - |InfallibleAllocPolicy| is another possibility; it allows the | |||
38 | // abovementioned OOM checks to be done with MOZ_ALWAYS_TRUE(). | |||
39 | // | |||
40 | // Note that entry storage allocation is lazy, and not done until the first | |||
41 | // lookupForAdd(), put(), or putNew() is performed. | |||
42 | // | |||
43 | // See AllocPolicy.h for more details. | |||
44 | // | |||
45 | // Documentation on how to use HashMap and HashSet, including examples, is | |||
46 | // present within those classes. Search for "class HashMap" and "class | |||
47 | // HashSet". | |||
48 | // | |||
49 | // Both HashMap and HashSet are implemented on top of a third class, HashTable. | |||
50 | // You only need to look at HashTable if you want to understand the | |||
51 | // implementation. | |||
52 | // | |||
53 | // How does mozilla::HashTable (this file) compare with PLDHashTable (and its | |||
54 | // subclasses, such as nsTHashtable)? | |||
55 | // | |||
56 | // - mozilla::HashTable is a lot faster, largely because it uses templates | |||
57 | // throughout *and* inlines everything. PLDHashTable inlines operations much | |||
58 | // less aggressively, and also uses "virtual ops" for operations like hashing | |||
59 | // and matching entries that require function calls. | |||
60 | // | |||
61 | // - Correspondingly, mozilla::HashTable use is likely to increase executable | |||
62 | // size much more than PLDHashTable. | |||
63 | // | |||
64 | // - mozilla::HashTable has a nicer API, with a proper HashSet vs. HashMap | |||
65 | // distinction. | |||
66 | // | |||
67 | // - mozilla::HashTable requires more explicit OOM checking. As mentioned | |||
68 | // above, the use of |InfallibleAllocPolicy| can simplify things. | |||
69 | // | |||
70 | // - mozilla::HashTable has a default capacity on creation of 32 and a minimum | |||
71 | // capacity of 4. PLDHashTable has a default capacity on creation of 8 and a | |||
72 | // minimum capacity of 8. | |||
73 | ||||
74 | #ifndef mozilla_HashTable_h | |||
75 | #define mozilla_HashTable_h | |||
76 | ||||
77 | #include <utility> | |||
78 | #include <type_traits> | |||
79 | ||||
80 | #include "mozilla/AllocPolicy.h" | |||
81 | #include "mozilla/Assertions.h" | |||
82 | #include "mozilla/Attributes.h" | |||
83 | #include "mozilla/Casting.h" | |||
84 | #include "mozilla/HashFunctions.h" | |||
85 | #include "mozilla/MathAlgorithms.h" | |||
86 | #include "mozilla/Maybe.h" | |||
87 | #include "mozilla/MemoryChecking.h" | |||
88 | #include "mozilla/MemoryReporting.h" | |||
89 | #include "mozilla/Opaque.h" | |||
90 | #include "mozilla/OperatorNewExtensions.h" | |||
91 | #include "mozilla/ReentrancyGuard.h" | |||
92 | #include "mozilla/UniquePtr.h" | |||
93 | #include "mozilla/WrappingOperations.h" | |||
94 | ||||
95 | namespace mozilla { | |||
96 | ||||
97 | template <class, class = void> | |||
98 | struct DefaultHasher; | |||
99 | ||||
100 | template <class, class> | |||
101 | class HashMapEntry; | |||
102 | ||||
103 | namespace detail { | |||
104 | ||||
105 | template <typename T> | |||
106 | class HashTableEntry; | |||
107 | ||||
108 | template <class T, class HashPolicy, class AllocPolicy> | |||
109 | class HashTable; | |||
110 | ||||
111 | } // namespace detail | |||
112 | ||||
113 | // The "generation" of a hash table is an opaque value indicating the state of | |||
114 | // modification of the hash table through its lifetime. If the generation of | |||
115 | // a hash table compares equal at times T1 and T2, then lookups in the hash | |||
116 | // table, pointers to (or into) hash table entries, etc. at time T1 are valid | |||
117 | // at time T2. If the generation compares unequal, these computations are all | |||
118 | // invalid and must be performed again to be used. | |||
119 | // | |||
120 | // Generations are meaningfully comparable only with respect to a single hash | |||
121 | // table. It's always nonsensical to compare the generation of distinct hash | |||
122 | // tables H1 and H2. | |||
123 | using Generation = Opaque<uint64_t>; | |||
124 | ||||
125 | //--------------------------------------------------------------------------- | |||
126 | // HashMap | |||
127 | //--------------------------------------------------------------------------- | |||
128 | ||||
129 | // HashMap is a fast hash-based map from keys to values. | |||
130 | // | |||
131 | // Template parameter requirements: | |||
132 | // - Key/Value: movable, destructible, assignable. | |||
133 | // - HashPolicy: see the "Hash Policy" section below. | |||
134 | // - AllocPolicy: see AllocPolicy.h. | |||
135 | // | |||
136 | // Note: | |||
137 | // - HashMap is not reentrant: Key/Value/HashPolicy/AllocPolicy members | |||
138 | // called by HashMap must not call back into the same HashMap object. | |||
139 | // | |||
140 | template <class Key, class Value, class HashPolicy = DefaultHasher<Key>, | |||
141 | class AllocPolicy = MallocAllocPolicy> | |||
142 | class HashMap { | |||
143 | // -- Implementation details ----------------------------------------------- | |||
144 | ||||
145 | // HashMap is not copyable or assignable. | |||
146 | HashMap(const HashMap& hm) = delete; | |||
147 | HashMap& operator=(const HashMap& hm) = delete; | |||
148 | ||||
149 | using TableEntry = HashMapEntry<Key, Value>; | |||
150 | ||||
151 | struct MapHashPolicy : HashPolicy { | |||
152 | using Base = HashPolicy; | |||
153 | using KeyType = Key; | |||
154 | ||||
155 | static const Key& getKey(TableEntry& aEntry) { return aEntry.key(); } | |||
156 | ||||
157 | static void setKey(TableEntry& aEntry, Key& aKey) { | |||
158 | HashPolicy::rekey(aEntry.mutableKey(), aKey); | |||
159 | } | |||
160 | }; | |||
161 | ||||
162 | using Impl = detail::HashTable<TableEntry, MapHashPolicy, AllocPolicy>; | |||
163 | Impl mImpl; | |||
164 | ||||
165 | friend class Impl::Enum; | |||
166 | ||||
167 | public: | |||
168 | using Lookup = typename HashPolicy::Lookup; | |||
169 | using Entry = TableEntry; | |||
170 | ||||
171 | // -- Initialization ------------------------------------------------------- | |||
172 | ||||
173 | explicit HashMap(AllocPolicy aAllocPolicy = AllocPolicy(), | |||
174 | uint32_t aLen = Impl::sDefaultLen) | |||
175 | : mImpl(std::move(aAllocPolicy), aLen) {} | |||
176 | ||||
177 | explicit HashMap(uint32_t aLen) : mImpl(AllocPolicy(), aLen) {} | |||
178 | ||||
179 | // HashMap is movable. | |||
180 | HashMap(HashMap&& aRhs) = default; | |||
181 | HashMap& operator=(HashMap&& aRhs) = default; | |||
182 | ||||
183 | // Swap the contents of this hash map with another. | |||
184 | void swap(HashMap& aOther) { mImpl.swap(aOther.mImpl); } | |||
185 | ||||
186 | // -- Status and sizing ---------------------------------------------------- | |||
187 | ||||
188 | // The map's current generation. | |||
189 | Generation generation() const { return mImpl.generation(); } | |||
190 | ||||
191 | // Is the map empty? | |||
192 | bool empty() const { return mImpl.empty(); } | |||
193 | ||||
194 | // Number of keys/values in the map. | |||
195 | uint32_t count() const { return mImpl.count(); } | |||
196 | ||||
197 | // Number of key/value slots in the map. Note: resize will happen well before | |||
198 | // count() == capacity(). | |||
199 | uint32_t capacity() const { return mImpl.capacity(); } | |||
200 | ||||
201 | // The size of the map's entry storage, in bytes. If the keys/values contain | |||
202 | // pointers to other heap blocks, you must iterate over the map and measure | |||
203 | // them separately; hence the "shallow" prefix. | |||
204 | size_t shallowSizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const { | |||
205 | return mImpl.shallowSizeOfExcludingThis(aMallocSizeOf); | |||
206 | } | |||
207 | size_t shallowSizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const { | |||
208 | return aMallocSizeOf(this) + | |||
209 | mImpl.shallowSizeOfExcludingThis(aMallocSizeOf); | |||
210 | } | |||
211 | ||||
212 | // Attempt to minimize the capacity(). If the table is empty, this will free | |||
213 | // the empty storage and upon regrowth it will be given the minimum capacity. | |||
214 | void compact() { mImpl.compact(); } | |||
215 | ||||
216 | // Attempt to reserve enough space to fit at least |aLen| elements. This is | |||
217 | // total capacity, including elements already present. Does nothing if the | |||
218 | // map already has sufficient capacity. | |||
219 | [[nodiscard]] bool reserve(uint32_t aLen) { return mImpl.reserve(aLen); } | |||
220 | ||||
221 | // -- Lookups -------------------------------------------------------------- | |||
222 | ||||
223 | // Does the map contain a key/value matching |aLookup|? | |||
224 | bool has(const Lookup& aLookup) const { | |||
225 | return mImpl.lookup(aLookup).found(); | |||
226 | } | |||
227 | ||||
228 | // Return a Ptr indicating whether a key/value matching |aLookup| is | |||
229 | // present in the map. E.g.: | |||
230 | // | |||
231 | // using HM = HashMap<int,char>; | |||
232 | // HM h; | |||
233 | // if (HM::Ptr p = h.lookup(3)) { | |||
234 | // assert(p->key() == 3); | |||
235 | // char val = p->value(); | |||
236 | // } | |||
237 | // | |||
238 | using Ptr = typename Impl::Ptr; | |||
239 | MOZ_ALWAYS_INLINEinline Ptr lookup(const Lookup& aLookup) const { | |||
240 | return mImpl.lookup(aLookup); | |||
241 | } | |||
242 | ||||
243 | // Like lookup(), but does not assert if two threads call it at the same | |||
244 | // time. Only use this method when none of the threads will modify the map. | |||
245 | MOZ_ALWAYS_INLINEinline Ptr readonlyThreadsafeLookup(const Lookup& aLookup) const { | |||
246 | return mImpl.readonlyThreadsafeLookup(aLookup); | |||
247 | } | |||
248 | ||||
249 | // -- Insertions ----------------------------------------------------------- | |||
250 | ||||
251 | // Overwrite existing value with |aValue|, or add it if not present. Returns | |||
252 | // false on OOM. | |||
253 | template <typename KeyInput, typename ValueInput> | |||
254 | [[nodiscard]] bool put(KeyInput&& aKey, ValueInput&& aValue) { | |||
255 | return put(aKey, std::forward<KeyInput>(aKey), | |||
256 | std::forward<ValueInput>(aValue)); | |||
257 | } | |||
258 | ||||
259 | template <typename KeyInput, typename ValueInput> | |||
260 | [[nodiscard]] bool put(const Lookup& aLookup, KeyInput&& aKey, | |||
261 | ValueInput&& aValue) { | |||
262 | AddPtr p = lookupForAdd(aLookup); | |||
263 | if (p) { | |||
264 | p->value() = std::forward<ValueInput>(aValue); | |||
265 | return true; | |||
266 | } | |||
267 | return add(p, std::forward<KeyInput>(aKey), | |||
268 | std::forward<ValueInput>(aValue)); | |||
269 | } | |||
270 | ||||
271 | // Like put(), but slightly faster. Must only be used when the given key is | |||
272 | // not already present. (In debug builds, assertions check this.) | |||
273 | template <typename KeyInput, typename ValueInput> | |||
274 | [[nodiscard]] bool putNew(KeyInput&& aKey, ValueInput&& aValue) { | |||
275 | return mImpl.putNew(aKey, std::forward<KeyInput>(aKey), | |||
276 | std::forward<ValueInput>(aValue)); | |||
277 | } | |||
278 | ||||
279 | template <typename KeyInput, typename ValueInput> | |||
280 | [[nodiscard]] bool putNew(const Lookup& aLookup, KeyInput&& aKey, | |||
281 | ValueInput&& aValue) { | |||
282 | return mImpl.putNew(aLookup, std::forward<KeyInput>(aKey), | |||
283 | std::forward<ValueInput>(aValue)); | |||
284 | } | |||
285 | ||||
286 | // Like putNew(), but should be only used when the table is known to be big | |||
287 | // enough for the insertion, and hashing cannot fail. Typically this is used | |||
288 | // to populate an empty map with known-unique keys after reserving space with | |||
289 | // reserve(), e.g. | |||
290 | // | |||
291 | // using HM = HashMap<int,char>; | |||
292 | // HM h; | |||
293 | // if (!h.reserve(3)) { | |||
294 | // MOZ_CRASH("OOM"); | |||
295 | // } | |||
296 | // h.putNewInfallible(1, 'a'); // unique key | |||
297 | // h.putNewInfallible(2, 'b'); // unique key | |||
298 | // h.putNewInfallible(3, 'c'); // unique key | |||
299 | // | |||
300 | template <typename KeyInput, typename ValueInput> | |||
301 | void putNewInfallible(KeyInput&& aKey, ValueInput&& aValue) { | |||
302 | mImpl.putNewInfallible(aKey, std::forward<KeyInput>(aKey), | |||
303 | std::forward<ValueInput>(aValue)); | |||
304 | } | |||
305 | ||||
306 | // Like |lookup(l)|, but on miss, |p = lookupForAdd(l)| allows efficient | |||
307 | // insertion of Key |k| (where |HashPolicy::match(k,l) == true|) using | |||
308 | // |add(p,k,v)|. After |add(p,k,v)|, |p| points to the new key/value. E.g.: | |||
309 | // | |||
310 | // using HM = HashMap<int,char>; | |||
311 | // HM h; | |||
312 | // HM::AddPtr p = h.lookupForAdd(3); | |||
313 | // if (!p) { | |||
314 | // if (!h.add(p, 3, 'a')) { | |||
315 | // return false; | |||
316 | // } | |||
317 | // } | |||
318 | // assert(p->key() == 3); | |||
319 | // char val = p->value(); | |||
320 | // | |||
321 | // N.B. The caller must ensure that no mutating hash table operations occur | |||
322 | // between a pair of lookupForAdd() and add() calls. To avoid looking up the | |||
323 | // key a second time, the caller may use the more efficient relookupOrAdd() | |||
324 | // method. This method reuses part of the hashing computation to more | |||
325 | // efficiently insert the key if it has not been added. For example, a | |||
326 | // mutation-handling version of the previous example: | |||
327 | // | |||
328 | // HM::AddPtr p = h.lookupForAdd(3); | |||
329 | // if (!p) { | |||
330 | // call_that_may_mutate_h(); | |||
331 | // if (!h.relookupOrAdd(p, 3, 'a')) { | |||
332 | // return false; | |||
333 | // } | |||
334 | // } | |||
335 | // assert(p->key() == 3); | |||
336 | // char val = p->value(); | |||
337 | // | |||
338 | using AddPtr = typename Impl::AddPtr; | |||
339 | MOZ_ALWAYS_INLINEinline AddPtr lookupForAdd(const Lookup& aLookup) { | |||
340 | return mImpl.lookupForAdd(aLookup); | |||
341 | } | |||
342 | ||||
343 | // Add a key/value. Returns false on OOM. | |||
344 | template <typename KeyInput, typename ValueInput> | |||
345 | [[nodiscard]] bool add(AddPtr& aPtr, KeyInput&& aKey, ValueInput&& aValue) { | |||
346 | return mImpl.add(aPtr, std::forward<KeyInput>(aKey), | |||
347 | std::forward<ValueInput>(aValue)); | |||
348 | } | |||
349 | ||||
350 | // See the comment above lookupForAdd() for details. | |||
351 | template <typename KeyInput, typename ValueInput> | |||
352 | [[nodiscard]] bool relookupOrAdd(AddPtr& aPtr, KeyInput&& aKey, | |||
353 | ValueInput&& aValue) { | |||
354 | return mImpl.relookupOrAdd(aPtr, aKey, std::forward<KeyInput>(aKey), | |||
355 | std::forward<ValueInput>(aValue)); | |||
356 | } | |||
357 | ||||
358 | // -- Removal -------------------------------------------------------------- | |||
359 | ||||
360 | // Lookup and remove the key/value matching |aLookup|, if present. | |||
361 | void remove(const Lookup& aLookup) { | |||
362 | if (Ptr p = lookup(aLookup)) { | |||
363 | remove(p); | |||
364 | } | |||
365 | } | |||
366 | ||||
367 | // Remove a previously found key/value (assuming aPtr.found()). The map must | |||
368 | // not have been mutated in the interim. | |||
369 | void remove(Ptr aPtr) { mImpl.remove(aPtr); } | |||
370 | ||||
371 | // Remove all keys/values without changing the capacity. | |||
372 | void clear() { mImpl.clear(); } | |||
373 | ||||
374 | // Like clear() followed by compact(). | |||
375 | void clearAndCompact() { mImpl.clearAndCompact(); } | |||
376 | ||||
377 | // -- Rekeying ------------------------------------------------------------- | |||
378 | ||||
379 | // Infallibly rekey one entry, if necessary. Requires that template | |||
380 | // parameters Key and HashPolicy::Lookup are the same type. | |||
381 | void rekeyIfMoved(const Key& aOldKey, const Key& aNewKey) { | |||
382 | if (aOldKey != aNewKey) { | |||
383 | rekeyAs(aOldKey, aNewKey, aNewKey); | |||
384 | } | |||
385 | } | |||
386 | ||||
387 | // Infallibly rekey one entry if present, and return whether that happened. | |||
388 | bool rekeyAs(const Lookup& aOldLookup, const Lookup& aNewLookup, | |||
389 | const Key& aNewKey) { | |||
390 | if (Ptr p = lookup(aOldLookup)) { | |||
391 | mImpl.rekeyAndMaybeRehash(p, aNewLookup, aNewKey); | |||
392 | return true; | |||
393 | } | |||
394 | return false; | |||
395 | } | |||
396 | ||||
397 | // -- Iteration ------------------------------------------------------------ | |||
398 | ||||
399 | // |iter()| returns an Iterator: | |||
400 | // | |||
401 | // HashMap<int, char> h; | |||
402 | // for (auto iter = h.iter(); !iter.done(); iter.next()) { | |||
403 | // char c = iter.get().value(); | |||
404 | // } | |||
405 | // | |||
406 | using Iterator = typename Impl::Iterator; | |||
407 | Iterator iter() const { return mImpl.iter(); } | |||
408 | ||||
409 | // |modIter()| returns a ModIterator: | |||
410 | // | |||
411 | // HashMap<int, char> h; | |||
412 | // for (auto iter = h.modIter(); !iter.done(); iter.next()) { | |||
413 | // if (iter.get().value() == 'l') { | |||
414 | // iter.remove(); | |||
415 | // } | |||
416 | // } | |||
417 | // | |||
418 | // Table resize may occur in ModIterator's destructor. | |||
419 | using ModIterator = typename Impl::ModIterator; | |||
420 | ModIterator modIter() { return mImpl.modIter(); } | |||
421 | ||||
422 | // These are similar to Iterator/ModIterator/iter(), but use different | |||
423 | // terminology. | |||
424 | using Range = typename Impl::Range; | |||
425 | using Enum = typename Impl::Enum; | |||
426 | Range all() const { return mImpl.all(); } | |||
427 | }; | |||
428 | ||||
429 | //--------------------------------------------------------------------------- | |||
430 | // HashSet | |||
431 | //--------------------------------------------------------------------------- | |||
432 | ||||
433 | // HashSet is a fast hash-based set of values. | |||
434 | // | |||
435 | // Template parameter requirements: | |||
436 | // - T: movable, destructible, assignable. | |||
437 | // - HashPolicy: see the "Hash Policy" section below. | |||
438 | // - AllocPolicy: see AllocPolicy.h | |||
439 | // | |||
440 | // Note: | |||
441 | // - HashSet is not reentrant: T/HashPolicy/AllocPolicy members called by | |||
442 | // HashSet must not call back into the same HashSet object. | |||
443 | // | |||
444 | template <class T, class HashPolicy = DefaultHasher<T>, | |||
445 | class AllocPolicy = MallocAllocPolicy> | |||
446 | class HashSet { | |||
447 | // -- Implementation details ----------------------------------------------- | |||
448 | ||||
449 | // HashSet is not copyable or assignable. | |||
450 | HashSet(const HashSet& hs) = delete; | |||
451 | HashSet& operator=(const HashSet& hs) = delete; | |||
452 | ||||
453 | struct SetHashPolicy : HashPolicy { | |||
454 | using Base = HashPolicy; | |||
455 | using KeyType = T; | |||
456 | ||||
457 | static const KeyType& getKey(const T& aT) { return aT; } | |||
458 | ||||
459 | static void setKey(T& aT, KeyType& aKey) { HashPolicy::rekey(aT, aKey); } | |||
460 | }; | |||
461 | ||||
462 | using Impl = detail::HashTable<const T, SetHashPolicy, AllocPolicy>; | |||
463 | Impl mImpl; | |||
464 | ||||
465 | friend class Impl::Enum; | |||
466 | ||||
467 | public: | |||
468 | using Lookup = typename HashPolicy::Lookup; | |||
469 | using Entry = T; | |||
470 | ||||
471 | // -- Initialization ------------------------------------------------------- | |||
472 | ||||
473 | explicit HashSet(AllocPolicy aAllocPolicy = AllocPolicy(), | |||
474 | uint32_t aLen = Impl::sDefaultLen) | |||
475 | : mImpl(std::move(aAllocPolicy), aLen) {} | |||
476 | ||||
477 | explicit HashSet(uint32_t aLen) : mImpl(AllocPolicy(), aLen) {} | |||
478 | ||||
479 | // HashSet is movable. | |||
480 | HashSet(HashSet&& aRhs) = default; | |||
481 | HashSet& operator=(HashSet&& aRhs) = default; | |||
482 | ||||
483 | // Swap the contents of this hash set with another. | |||
484 | void swap(HashSet& aOther) { mImpl.swap(aOther.mImpl); } | |||
485 | ||||
486 | // -- Status and sizing ---------------------------------------------------- | |||
487 | ||||
488 | // The set's current generation. | |||
489 | Generation generation() const { return mImpl.generation(); } | |||
490 | ||||
491 | // Is the set empty? | |||
492 | bool empty() const { return mImpl.empty(); } | |||
493 | ||||
494 | // Number of elements in the set. | |||
495 | uint32_t count() const { return mImpl.count(); } | |||
496 | ||||
497 | // Number of element slots in the set. Note: resize will happen well before | |||
498 | // count() == capacity(). | |||
499 | uint32_t capacity() const { return mImpl.capacity(); } | |||
500 | ||||
501 | // The size of the set's entry storage, in bytes. If the elements contain | |||
502 | // pointers to other heap blocks, you must iterate over the set and measure | |||
503 | // them separately; hence the "shallow" prefix. | |||
504 | size_t shallowSizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const { | |||
505 | return mImpl.shallowSizeOfExcludingThis(aMallocSizeOf); | |||
506 | } | |||
507 | size_t shallowSizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const { | |||
508 | return aMallocSizeOf(this) + | |||
509 | mImpl.shallowSizeOfExcludingThis(aMallocSizeOf); | |||
510 | } | |||
511 | ||||
512 | // Attempt to minimize the capacity(). If the table is empty, this will free | |||
513 | // the empty storage and upon regrowth it will be given the minimum capacity. | |||
514 | void compact() { mImpl.compact(); } | |||
515 | ||||
516 | // Attempt to reserve enough space to fit at least |aLen| elements. This is | |||
517 | // total capacity, including elements already present. Does nothing if the | |||
518 | // map already has sufficient capacity. | |||
519 | [[nodiscard]] bool reserve(uint32_t aLen) { return mImpl.reserve(aLen); } | |||
520 | ||||
521 | // -- Lookups -------------------------------------------------------------- | |||
522 | ||||
523 | // Does the set contain an element matching |aLookup|? | |||
524 | bool has(const Lookup& aLookup) const { | |||
525 | return mImpl.lookup(aLookup).found(); | |||
526 | } | |||
527 | ||||
528 | // Return a Ptr indicating whether an element matching |aLookup| is present | |||
529 | // in the set. E.g.: | |||
530 | // | |||
531 | // using HS = HashSet<int>; | |||
532 | // HS h; | |||
533 | // if (HS::Ptr p = h.lookup(3)) { | |||
534 | // assert(*p == 3); // p acts like a pointer to int | |||
535 | // } | |||
536 | // | |||
537 | using Ptr = typename Impl::Ptr; | |||
538 | MOZ_ALWAYS_INLINEinline Ptr lookup(const Lookup& aLookup) const { | |||
539 | return mImpl.lookup(aLookup); | |||
540 | } | |||
541 | ||||
542 | // Like lookup(), but does not assert if two threads call it at the same | |||
543 | // time. Only use this method when none of the threads will modify the set. | |||
544 | MOZ_ALWAYS_INLINEinline Ptr readonlyThreadsafeLookup(const Lookup& aLookup) const { | |||
545 | return mImpl.readonlyThreadsafeLookup(aLookup); | |||
546 | } | |||
547 | ||||
548 | // -- Insertions ----------------------------------------------------------- | |||
549 | ||||
550 | // Add |aU| if it is not present already. Returns false on OOM. | |||
551 | template <typename U> | |||
552 | [[nodiscard]] bool put(U&& aU) { | |||
553 | AddPtr p = lookupForAdd(aU); | |||
554 | return p ? true : add(p, std::forward<U>(aU)); | |||
555 | } | |||
556 | ||||
557 | // Like put(), but slightly faster. Must only be used when the given element | |||
558 | // is not already present. (In debug builds, assertions check this.) | |||
559 | template <typename U> | |||
560 | [[nodiscard]] bool putNew(U&& aU) { | |||
561 | return mImpl.putNew(aU, std::forward<U>(aU)); | |||
562 | } | |||
563 | ||||
564 | // Like the other putNew(), but for when |Lookup| is different to |T|. | |||
565 | template <typename U> | |||
566 | [[nodiscard]] bool putNew(const Lookup& aLookup, U&& aU) { | |||
567 | return mImpl.putNew(aLookup, std::forward<U>(aU)); | |||
568 | } | |||
569 | ||||
570 | // Like putNew(), but should be only used when the table is known to be big | |||
571 | // enough for the insertion, and hashing cannot fail. Typically this is used | |||
572 | // to populate an empty set with known-unique elements after reserving space | |||
573 | // with reserve(), e.g. | |||
574 | // | |||
575 | // using HS = HashMap<int>; | |||
576 | // HS h; | |||
577 | // if (!h.reserve(3)) { | |||
578 | // MOZ_CRASH("OOM"); | |||
579 | // } | |||
580 | // h.putNewInfallible(1); // unique element | |||
581 | // h.putNewInfallible(2); // unique element | |||
582 | // h.putNewInfallible(3); // unique element | |||
583 | // | |||
584 | template <typename U> | |||
585 | void putNewInfallible(const Lookup& aLookup, U&& aU) { | |||
586 | mImpl.putNewInfallible(aLookup, std::forward<U>(aU)); | |||
587 | } | |||
588 | ||||
589 | // Like |lookup(l)|, but on miss, |p = lookupForAdd(l)| allows efficient | |||
590 | // insertion of T value |t| (where |HashPolicy::match(t,l) == true|) using | |||
591 | // |add(p,t)|. After |add(p,t)|, |p| points to the new element. E.g.: | |||
592 | // | |||
593 | // using HS = HashSet<int>; | |||
594 | // HS h; | |||
595 | // HS::AddPtr p = h.lookupForAdd(3); | |||
596 | // if (!p) { | |||
597 | // if (!h.add(p, 3)) { | |||
598 | // return false; | |||
599 | // } | |||
600 | // } | |||
601 | // assert(*p == 3); // p acts like a pointer to int | |||
602 | // | |||
603 | // N.B. The caller must ensure that no mutating hash table operations occur | |||
604 | // between a pair of lookupForAdd() and add() calls. To avoid looking up the | |||
605 | // key a second time, the caller may use the more efficient relookupOrAdd() | |||
606 | // method. This method reuses part of the hashing computation to more | |||
607 | // efficiently insert the key if it has not been added. For example, a | |||
608 | // mutation-handling version of the previous example: | |||
609 | // | |||
610 | // HS::AddPtr p = h.lookupForAdd(3); | |||
611 | // if (!p) { | |||
612 | // call_that_may_mutate_h(); | |||
613 | // if (!h.relookupOrAdd(p, 3, 3)) { | |||
614 | // return false; | |||
615 | // } | |||
616 | // } | |||
617 | // assert(*p == 3); | |||
618 | // | |||
619 | // Note that relookupOrAdd(p,l,t) performs Lookup using |l| and adds the | |||
620 | // entry |t|, where the caller ensures match(l,t). | |||
621 | using AddPtr = typename Impl::AddPtr; | |||
622 | MOZ_ALWAYS_INLINEinline AddPtr lookupForAdd(const Lookup& aLookup) { | |||
623 | return mImpl.lookupForAdd(aLookup); | |||
624 | } | |||
625 | ||||
626 | // Add an element. Returns false on OOM. | |||
627 | template <typename U> | |||
628 | [[nodiscard]] bool add(AddPtr& aPtr, U&& aU) { | |||
629 | return mImpl.add(aPtr, std::forward<U>(aU)); | |||
630 | } | |||
631 | ||||
632 | // See the comment above lookupForAdd() for details. | |||
633 | template <typename U> | |||
634 | [[nodiscard]] bool relookupOrAdd(AddPtr& aPtr, const Lookup& aLookup, | |||
635 | U&& aU) { | |||
636 | return mImpl.relookupOrAdd(aPtr, aLookup, std::forward<U>(aU)); | |||
637 | } | |||
638 | ||||
639 | // -- Removal -------------------------------------------------------------- | |||
640 | ||||
641 | // Lookup and remove the element matching |aLookup|, if present. | |||
642 | void remove(const Lookup& aLookup) { | |||
643 | if (Ptr p = lookup(aLookup)) { | |||
644 | remove(p); | |||
645 | } | |||
646 | } | |||
647 | ||||
648 | // Remove a previously found element (assuming aPtr.found()). The set must | |||
649 | // not have been mutated in the interim. | |||
650 | void remove(Ptr aPtr) { mImpl.remove(aPtr); } | |||
651 | ||||
652 | // Remove all keys/values without changing the capacity. | |||
653 | void clear() { mImpl.clear(); } | |||
654 | ||||
655 | // Like clear() followed by compact(). | |||
656 | void clearAndCompact() { mImpl.clearAndCompact(); } | |||
657 | ||||
658 | // -- Rekeying ------------------------------------------------------------- | |||
659 | ||||
660 | // Infallibly rekey one entry, if present. Requires that template parameters | |||
661 | // T and HashPolicy::Lookup are the same type. | |||
662 | void rekeyIfMoved(const Lookup& aOldValue, const T& aNewValue) { | |||
663 | if (aOldValue != aNewValue) { | |||
664 | rekeyAs(aOldValue, aNewValue, aNewValue); | |||
665 | } | |||
666 | } | |||
667 | ||||
668 | // Infallibly rekey one entry if present, and return whether that happened. | |||
669 | bool rekeyAs(const Lookup& aOldLookup, const Lookup& aNewLookup, | |||
670 | const T& aNewValue) { | |||
671 | if (Ptr p = lookup(aOldLookup)) { | |||
672 | mImpl.rekeyAndMaybeRehash(p, aNewLookup, aNewValue); | |||
673 | return true; | |||
674 | } | |||
675 | return false; | |||
676 | } | |||
677 | ||||
678 | // Infallibly replace the current key at |aPtr| with an equivalent key. | |||
679 | // Specifically, both HashPolicy::hash and HashPolicy::match must return | |||
680 | // identical results for the new and old key when applied against all | |||
681 | // possible matching values. | |||
682 | void replaceKey(Ptr aPtr, const Lookup& aLookup, const T& aNewValue) { | |||
683 | MOZ_ASSERT(aPtr.found())do { static_assert( mozilla::detail::AssertionConditionType< decltype(aPtr.found())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(aPtr.found()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("aPtr.found()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 683); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPtr.found()" ")"); do { *((volatile int*)__null) = 683; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
684 | MOZ_ASSERT(*aPtr != aNewValue)do { static_assert( mozilla::detail::AssertionConditionType< decltype(*aPtr != aNewValue)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(*aPtr != aNewValue))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("*aPtr != aNewValue" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 684); AnnotateMozCrashReason("MOZ_ASSERT" "(" "*aPtr != aNewValue" ")"); do { *((volatile int*)__null) = 684; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
685 | MOZ_ASSERT(HashPolicy::match(*aPtr, aLookup))do { static_assert( mozilla::detail::AssertionConditionType< decltype(HashPolicy::match(*aPtr, aLookup))>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(HashPolicy::match(*aPtr, aLookup )))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("HashPolicy::match(*aPtr, aLookup)", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 685); AnnotateMozCrashReason("MOZ_ASSERT" "(" "HashPolicy::match(*aPtr, aLookup)" ")"); do { *((volatile int*)__null) = 685; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
686 | MOZ_ASSERT(HashPolicy::match(aNewValue, aLookup))do { static_assert( mozilla::detail::AssertionConditionType< decltype(HashPolicy::match(aNewValue, aLookup))>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(! !(HashPolicy::match(aNewValue, aLookup)))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("HashPolicy::match(aNewValue, aLookup)" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 686); AnnotateMozCrashReason("MOZ_ASSERT" "(" "HashPolicy::match(aNewValue, aLookup)" ")"); do { *((volatile int*)__null) = 686; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
687 | const_cast<T&>(*aPtr) = aNewValue; | |||
688 | MOZ_ASSERT(*lookup(aLookup) == aNewValue)do { static_assert( mozilla::detail::AssertionConditionType< decltype(*lookup(aLookup) == aNewValue)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(*lookup(aLookup) == aNewValue ))), 0))) { do { } while (false); MOZ_ReportAssertionFailure( "*lookup(aLookup) == aNewValue", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 688); AnnotateMozCrashReason("MOZ_ASSERT" "(" "*lookup(aLookup) == aNewValue" ")"); do { *((volatile int*)__null) = 688; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
689 | } | |||
690 | void replaceKey(Ptr aPtr, const T& aNewValue) { | |||
691 | replaceKey(aPtr, aNewValue, aNewValue); | |||
692 | } | |||
693 | ||||
694 | // -- Iteration ------------------------------------------------------------ | |||
695 | ||||
696 | // |iter()| returns an Iterator: | |||
697 | // | |||
698 | // HashSet<int> h; | |||
699 | // for (auto iter = h.iter(); !iter.done(); iter.next()) { | |||
700 | // int i = iter.get(); | |||
701 | // } | |||
702 | // | |||
703 | using Iterator = typename Impl::Iterator; | |||
704 | Iterator iter() const { return mImpl.iter(); } | |||
705 | ||||
706 | // |modIter()| returns a ModIterator: | |||
707 | // | |||
708 | // HashSet<int> h; | |||
709 | // for (auto iter = h.modIter(); !iter.done(); iter.next()) { | |||
710 | // if (iter.get() == 42) { | |||
711 | // iter.remove(); | |||
712 | // } | |||
713 | // } | |||
714 | // | |||
715 | // Table resize may occur in ModIterator's destructor. | |||
716 | using ModIterator = typename Impl::ModIterator; | |||
717 | ModIterator modIter() { return mImpl.modIter(); } | |||
718 | ||||
719 | // These are similar to Iterator/ModIterator/iter(), but use different | |||
720 | // terminology. | |||
721 | using Range = typename Impl::Range; | |||
722 | using Enum = typename Impl::Enum; | |||
723 | Range all() const { return mImpl.all(); } | |||
724 | }; | |||
725 | ||||
726 | //--------------------------------------------------------------------------- | |||
727 | // Hash Policy | |||
728 | //--------------------------------------------------------------------------- | |||
729 | ||||
730 | // A hash policy |HP| for a hash table with key-type |Key| must provide: | |||
731 | // | |||
732 | // - a type |HP::Lookup| to use to lookup table entries; | |||
733 | // | |||
734 | // - a static member function |HP::hash| that hashes lookup values: | |||
735 | // | |||
736 | // static mozilla::HashNumber hash(const Lookup&); | |||
737 | // | |||
738 | // - a static member function |HP::match| that tests equality of key and | |||
739 | // lookup values: | |||
740 | // | |||
741 | // static bool match(const Key& aKey, const Lookup& aLookup); | |||
742 | // | |||
743 | // |aKey| and |aLookup| can have different hash numbers, only when a | |||
744 | // collision happens with |prepareHash| operation, which is less frequent. | |||
745 | // Thus, |HP::match| shouldn't assume the hash equality in the comparison, | |||
746 | // even if the hash numbers are almost always same between them. | |||
747 | // | |||
748 | // Normally, Lookup = Key. In general, though, different values and types of | |||
749 | // values can be used to lookup and store. If a Lookup value |l| is not equal | |||
750 | // to the added Key value |k|, the user must ensure that |HP::match(k,l)| is | |||
751 | // true. E.g.: | |||
752 | // | |||
753 | // mozilla::HashSet<Key, HP>::AddPtr p = h.lookup(l); | |||
754 | // if (!p) { | |||
755 | // assert(HP::match(k, l)); // must hold | |||
756 | // h.add(p, k); | |||
757 | // } | |||
758 | ||||
759 | // A pointer hashing policy that uses HashGeneric() to create good hashes for | |||
760 | // pointers. Note that we don't shift out the lowest k bits because we don't | |||
761 | // want to assume anything about the alignment of the pointers. | |||
762 | template <typename Key> | |||
763 | struct PointerHasher { | |||
764 | using Lookup = Key; | |||
765 | ||||
766 | static HashNumber hash(const Lookup& aLookup) { return HashGeneric(aLookup); } | |||
767 | ||||
768 | static bool match(const Key& aKey, const Lookup& aLookup) { | |||
769 | return aKey == aLookup; | |||
770 | } | |||
771 | ||||
772 | static void rekey(Key& aKey, const Key& aNewKey) { aKey = aNewKey; } | |||
773 | }; | |||
774 | ||||
775 | // The default hash policy, which only works with integers. | |||
776 | template <class Key, typename> | |||
777 | struct DefaultHasher { | |||
778 | using Lookup = Key; | |||
779 | ||||
780 | static HashNumber hash(const Lookup& aLookup) { | |||
781 | // Just convert the integer to a HashNumber and use that as is. (This | |||
782 | // discards the high 32-bits of 64-bit integers!) ScrambleHashCode() is | |||
783 | // subsequently called on the value to improve the distribution. | |||
784 | return aLookup; | |||
785 | } | |||
786 | ||||
787 | static bool match(const Key& aKey, const Lookup& aLookup) { | |||
788 | // Use builtin or overloaded operator==. | |||
789 | return aKey == aLookup; | |||
790 | } | |||
791 | ||||
792 | static void rekey(Key& aKey, const Key& aNewKey) { aKey = aNewKey; } | |||
793 | }; | |||
794 | ||||
795 | // A DefaultHasher specialization for enums. | |||
796 | template <class T> | |||
797 | struct DefaultHasher<T, std::enable_if_t<std::is_enum_v<T>>> { | |||
798 | using Key = T; | |||
799 | using Lookup = Key; | |||
800 | ||||
801 | static HashNumber hash(const Lookup& aLookup) { return HashGeneric(aLookup); } | |||
802 | ||||
803 | static bool match(const Key& aKey, const Lookup& aLookup) { | |||
804 | // Use builtin or overloaded operator==. | |||
805 | return aKey == static_cast<Key>(aLookup); | |||
806 | } | |||
807 | ||||
808 | static void rekey(Key& aKey, const Key& aNewKey) { aKey = aNewKey; } | |||
809 | }; | |||
810 | ||||
811 | // A DefaultHasher specialization for pointers. | |||
812 | template <class T> | |||
813 | struct DefaultHasher<T*> : PointerHasher<T*> {}; | |||
814 | ||||
815 | // A DefaultHasher specialization for mozilla::UniquePtr. | |||
816 | template <class T, class D> | |||
817 | struct DefaultHasher<UniquePtr<T, D>> { | |||
818 | using Key = UniquePtr<T, D>; | |||
819 | using Lookup = Key; | |||
820 | using PtrHasher = PointerHasher<T*>; | |||
821 | ||||
822 | static HashNumber hash(const Lookup& aLookup) { | |||
823 | return PtrHasher::hash(aLookup.get()); | |||
824 | } | |||
825 | ||||
826 | static bool match(const Key& aKey, const Lookup& aLookup) { | |||
827 | return PtrHasher::match(aKey.get(), aLookup.get()); | |||
828 | } | |||
829 | ||||
830 | static void rekey(UniquePtr<T, D>& aKey, UniquePtr<T, D>&& aNewKey) { | |||
831 | aKey = std::move(aNewKey); | |||
832 | } | |||
833 | }; | |||
834 | ||||
835 | // A DefaultHasher specialization for doubles. | |||
836 | template <> | |||
837 | struct DefaultHasher<double> { | |||
838 | using Key = double; | |||
839 | using Lookup = Key; | |||
840 | ||||
841 | static HashNumber hash(const Lookup& aLookup) { | |||
842 | // Just xor the high bits with the low bits, and then treat the bits of the | |||
843 | // result as a uint32_t. | |||
844 | static_assert(sizeof(HashNumber) == 4, | |||
845 | "subsequent code assumes a four-byte hash"); | |||
846 | uint64_t u = BitwiseCast<uint64_t>(aLookup); | |||
847 | return HashNumber(u ^ (u >> 32)); | |||
848 | } | |||
849 | ||||
850 | static bool match(const Key& aKey, const Lookup& aLookup) { | |||
851 | return BitwiseCast<uint64_t>(aKey) == BitwiseCast<uint64_t>(aLookup); | |||
852 | } | |||
853 | }; | |||
854 | ||||
855 | // A DefaultHasher specialization for floats. | |||
856 | template <> | |||
857 | struct DefaultHasher<float> { | |||
858 | using Key = float; | |||
859 | using Lookup = Key; | |||
860 | ||||
861 | static HashNumber hash(const Lookup& aLookup) { | |||
862 | // Just use the value as if its bits form an integer. ScrambleHashCode() is | |||
863 | // subsequently called on the value to improve the distribution. | |||
864 | static_assert(sizeof(HashNumber) == 4, | |||
865 | "subsequent code assumes a four-byte hash"); | |||
866 | return HashNumber(BitwiseCast<uint32_t>(aLookup)); | |||
867 | } | |||
868 | ||||
869 | static bool match(const Key& aKey, const Lookup& aLookup) { | |||
870 | return BitwiseCast<uint32_t>(aKey) == BitwiseCast<uint32_t>(aLookup); | |||
871 | } | |||
872 | }; | |||
873 | ||||
874 | // A hash policy for C strings. | |||
875 | struct CStringHasher { | |||
876 | using Key = const char*; | |||
877 | using Lookup = const char*; | |||
878 | ||||
879 | static HashNumber hash(const Lookup& aLookup) { return HashString(aLookup); } | |||
880 | ||||
881 | static bool match(const Key& aKey, const Lookup& aLookup) { | |||
882 | return strcmp(aKey, aLookup) == 0; | |||
883 | } | |||
884 | }; | |||
885 | ||||
886 | //--------------------------------------------------------------------------- | |||
887 | // Fallible Hashing Interface | |||
888 | //--------------------------------------------------------------------------- | |||
889 | ||||
890 | // Most of the time generating a hash code is infallible, but sometimes it is | |||
891 | // necessary to generate hash codes on demand in a way that can fail. Specialize | |||
892 | // this class for your own hash policy to provide fallible hashing. | |||
893 | // | |||
894 | // This is used by MovableCellHasher to handle the fact that generating a unique | |||
895 | // ID for cell pointer may fail due to OOM. | |||
896 | // | |||
897 | // The default implementations of these methods delegate to the usual HashPolicy | |||
898 | // implementation and always succeed. | |||
899 | template <typename HashPolicy> | |||
900 | struct FallibleHashMethods { | |||
901 | // Return true if a hashcode is already available for its argument, and | |||
902 | // sets |aHashOut|. Once this succeeds for a specific argument it | |||
903 | // must continue to do so. | |||
904 | // | |||
905 | // Return false if a hashcode is not already available. This implies that any | |||
906 | // lookup must fail, as the hash code would have to have been successfully | |||
907 | // created on insertion. | |||
908 | template <typename Lookup> | |||
909 | static bool maybeGetHash(Lookup&& aLookup, HashNumber* aHashOut) { | |||
910 | *aHashOut = HashPolicy::hash(aLookup); | |||
911 | return true; | |||
912 | } | |||
913 | ||||
914 | // Fallible method to ensure a hashcode exists for its argument and create one | |||
915 | // if not. Sets |aHashOut| to the hashcode and retuns true on success. Returns | |||
916 | // false on error, e.g. out of memory. | |||
917 | template <typename Lookup> | |||
918 | static bool ensureHash(Lookup&& aLookup, HashNumber* aHashOut) { | |||
919 | *aHashOut = HashPolicy::hash(aLookup); | |||
920 | return true; | |||
921 | } | |||
922 | }; | |||
923 | ||||
924 | template <typename HashPolicy, typename Lookup> | |||
925 | static bool MaybeGetHash(Lookup&& aLookup, HashNumber* aHashOut) { | |||
926 | return FallibleHashMethods<typename HashPolicy::Base>::maybeGetHash( | |||
927 | std::forward<Lookup>(aLookup), aHashOut); | |||
928 | } | |||
929 | ||||
930 | template <typename HashPolicy, typename Lookup> | |||
931 | static bool EnsureHash(Lookup&& aLookup, HashNumber* aHashOut) { | |||
932 | return FallibleHashMethods<typename HashPolicy::Base>::ensureHash( | |||
933 | std::forward<Lookup>(aLookup), aHashOut); | |||
934 | } | |||
935 | ||||
936 | //--------------------------------------------------------------------------- | |||
937 | // Implementation Details (HashMapEntry, HashTableEntry, HashTable) | |||
938 | //--------------------------------------------------------------------------- | |||
939 | ||||
940 | // Both HashMap and HashSet are implemented by a single HashTable that is even | |||
941 | // more heavily parameterized than the other two. This leaves HashTable gnarly | |||
942 | // and extremely coupled to HashMap and HashSet; thus code should not use | |||
943 | // HashTable directly. | |||
944 | ||||
945 | template <class Key, class Value> | |||
946 | class HashMapEntry { | |||
947 | Key key_; | |||
948 | Value value_; | |||
949 | ||||
950 | template <class, class, class> | |||
951 | friend class detail::HashTable; | |||
952 | template <class> | |||
953 | friend class detail::HashTableEntry; | |||
954 | template <class, class, class, class> | |||
955 | friend class HashMap; | |||
956 | ||||
957 | public: | |||
958 | template <typename KeyInput, typename ValueInput> | |||
959 | HashMapEntry(KeyInput&& aKey, ValueInput&& aValue) | |||
960 | : key_(std::forward<KeyInput>(aKey)), | |||
961 | value_(std::forward<ValueInput>(aValue)) {} | |||
962 | ||||
963 | HashMapEntry(HashMapEntry&& aRhs) = default; | |||
964 | HashMapEntry& operator=(HashMapEntry&& aRhs) = default; | |||
965 | ||||
966 | using KeyType = Key; | |||
967 | using ValueType = Value; | |||
968 | ||||
969 | const Key& key() const { return key_; } | |||
970 | ||||
971 | // Use this method with caution! If the key is changed such that its hash | |||
972 | // value also changes, the map will be left in an invalid state. | |||
973 | Key& mutableKey() { return key_; } | |||
974 | ||||
975 | const Value& value() const { return value_; } | |||
976 | Value& value() { return value_; } | |||
977 | ||||
978 | private: | |||
979 | HashMapEntry(const HashMapEntry&) = delete; | |||
980 | void operator=(const HashMapEntry&) = delete; | |||
981 | }; | |||
982 | ||||
983 | namespace detail { | |||
984 | ||||
985 | template <class T, class HashPolicy, class AllocPolicy> | |||
986 | class HashTable; | |||
987 | ||||
988 | template <typename T> | |||
989 | class EntrySlot; | |||
990 | ||||
991 | template <typename T> | |||
992 | class HashTableEntry { | |||
993 | private: | |||
994 | using NonConstT = std::remove_const_t<T>; | |||
995 | ||||
996 | // Instead of having a hash table entry store that looks like this: | |||
997 | // | |||
998 | // +--------+--------+--------+--------+ | |||
999 | // | entry0 | entry1 | .... | entryN | | |||
1000 | // +--------+--------+--------+--------+ | |||
1001 | // | |||
1002 | // where the entries contained their cached hash code, we're going to lay out | |||
1003 | // the entry store thusly: | |||
1004 | // | |||
1005 | // +-------+-------+-------+-------+--------+--------+--------+--------+ | |||
1006 | // | hash0 | hash1 | ... | hashN | entry0 | entry1 | .... | entryN | | |||
1007 | // +-------+-------+-------+-------+--------+--------+--------+--------+ | |||
1008 | // | |||
1009 | // with all the cached hashes prior to the actual entries themselves. | |||
1010 | // | |||
1011 | // We do this because implementing the first strategy requires us to make | |||
1012 | // HashTableEntry look roughly like: | |||
1013 | // | |||
1014 | // template <typename T> | |||
1015 | // class HashTableEntry { | |||
1016 | // HashNumber mKeyHash; | |||
1017 | // T mValue; | |||
1018 | // }; | |||
1019 | // | |||
1020 | // The problem with this setup is that, depending on the layout of `T`, there | |||
1021 | // may be platform ABI-mandated padding between `mKeyHash` and the first | |||
1022 | // member of `T`. This ABI-mandated padding is wasted space, and can be | |||
1023 | // surprisingly common, e.g. when `T` is a single pointer on 64-bit platforms. | |||
1024 | // In such cases, we're throwing away a quarter of our entry store on padding, | |||
1025 | // which is undesirable. | |||
1026 | // | |||
1027 | // The second layout above, namely: | |||
1028 | // | |||
1029 | // +-------+-------+-------+-------+--------+--------+--------+--------+ | |||
1030 | // | hash0 | hash1 | ... | hashN | entry0 | entry1 | .... | entryN | | |||
1031 | // +-------+-------+-------+-------+--------+--------+--------+--------+ | |||
1032 | // | |||
1033 | // means there is no wasted space between the hashes themselves, and no wasted | |||
1034 | // space between the entries themselves. However, we would also like there to | |||
1035 | // be no gap between the last hash and the first entry. The memory allocator | |||
1036 | // guarantees the alignment of the start of the hashes. The use of a | |||
1037 | // power-of-two capacity of at least 4 guarantees that the alignment of the | |||
1038 | // *end* of the hash array is no less than the alignment of the start. | |||
1039 | // Finally, the static_asserts here guarantee that the entries themselves | |||
1040 | // don't need to be any more aligned than the alignment of the entry store | |||
1041 | // itself. | |||
1042 | // | |||
1043 | // This assertion is safe for 32-bit builds because on both Windows and Linux | |||
1044 | // (including Android), the minimum alignment for allocations larger than 8 | |||
1045 | // bytes is 8 bytes, and the actual data for entries in our entry store is | |||
1046 | // guaranteed to have that alignment as well, thanks to the power-of-two | |||
1047 | // number of cached hash values stored prior to the entry data. | |||
1048 | ||||
1049 | // The allocation policy must allocate a table with at least this much | |||
1050 | // alignment. | |||
1051 | static constexpr size_t kMinimumAlignment = 8; | |||
1052 | ||||
1053 | static_assert(alignof(HashNumber) <= kMinimumAlignment, | |||
1054 | "[N*2 hashes, N*2 T values] allocation's alignment must be " | |||
1055 | "enough to align each hash"); | |||
1056 | static_assert(alignof(NonConstT) <= 2 * sizeof(HashNumber), | |||
1057 | "subsequent N*2 T values must not require more than an even " | |||
1058 | "number of HashNumbers provides"); | |||
1059 | ||||
1060 | static const HashNumber sFreeKey = 0; | |||
1061 | static const HashNumber sRemovedKey = 1; | |||
1062 | static const HashNumber sCollisionBit = 1; | |||
1063 | ||||
1064 | alignas(NonConstT) unsigned char mValueData[sizeof(NonConstT)]; | |||
1065 | ||||
1066 | private: | |||
1067 | template <class, class, class> | |||
1068 | friend class HashTable; | |||
1069 | template <typename> | |||
1070 | friend class EntrySlot; | |||
1071 | ||||
1072 | // Some versions of GCC treat it as a -Wstrict-aliasing violation (ergo a | |||
1073 | // -Werror compile error) to reinterpret_cast<> |mValueData| to |T*|, even | |||
1074 | // through |void*|. Placing the latter cast in these separate functions | |||
1075 | // breaks the chain such that affected GCC versions no longer warn/error. | |||
1076 | void* rawValuePtr() { return mValueData; } | |||
1077 | ||||
1078 | static bool isLiveHash(HashNumber hash) { return hash > sRemovedKey; } | |||
1079 | ||||
1080 | HashTableEntry(const HashTableEntry&) = delete; | |||
1081 | void operator=(const HashTableEntry&) = delete; | |||
1082 | ||||
1083 | NonConstT* valuePtr() { return reinterpret_cast<NonConstT*>(rawValuePtr()); } | |||
1084 | ||||
1085 | void destroyStoredT() { | |||
1086 | NonConstT* ptr = valuePtr(); | |||
1087 | ptr->~T(); | |||
1088 | MOZ_MAKE_MEM_UNDEFINED(ptr, sizeof(*ptr))do { } while (0); | |||
1089 | } | |||
1090 | ||||
1091 | public: | |||
1092 | HashTableEntry() = default; | |||
1093 | ||||
1094 | ~HashTableEntry() { MOZ_MAKE_MEM_UNDEFINED(this, sizeof(*this))do { } while (0); } | |||
1095 | ||||
1096 | void destroy() { destroyStoredT(); } | |||
1097 | ||||
1098 | void swap(HashTableEntry* aOther, bool aIsLive) { | |||
1099 | // This allows types to use Argument-Dependent-Lookup, and thus use a custom | |||
1100 | // std::swap, which is needed by types like JS::Heap and such. | |||
1101 | using std::swap; | |||
1102 | ||||
1103 | if (this == aOther) { | |||
1104 | return; | |||
1105 | } | |||
1106 | if (aIsLive) { | |||
1107 | swap(*valuePtr(), *aOther->valuePtr()); | |||
1108 | } else { | |||
1109 | *aOther->valuePtr() = std::move(*valuePtr()); | |||
1110 | destroy(); | |||
1111 | } | |||
1112 | } | |||
1113 | ||||
1114 | T& get() { return *valuePtr(); } | |||
1115 | ||||
1116 | NonConstT& getMutable() { return *valuePtr(); } | |||
1117 | }; | |||
1118 | ||||
1119 | // A slot represents a cached hash value and its associated entry stored | |||
1120 | // in the hash table. These two things are not stored in contiguous memory. | |||
1121 | template <class T> | |||
1122 | class EntrySlot { | |||
1123 | using NonConstT = std::remove_const_t<T>; | |||
1124 | ||||
1125 | using Entry = HashTableEntry<T>; | |||
1126 | ||||
1127 | Entry* mEntry; | |||
1128 | HashNumber* mKeyHash; | |||
1129 | ||||
1130 | template <class, class, class> | |||
1131 | friend class HashTable; | |||
1132 | ||||
1133 | EntrySlot(Entry* aEntry, HashNumber* aKeyHash) | |||
1134 | : mEntry(aEntry), mKeyHash(aKeyHash) {} | |||
1135 | ||||
1136 | public: | |||
1137 | static bool isLiveHash(HashNumber hash) { return hash > Entry::sRemovedKey; } | |||
1138 | ||||
1139 | EntrySlot(const EntrySlot&) = default; | |||
1140 | EntrySlot(EntrySlot&& aOther) = default; | |||
1141 | ||||
1142 | EntrySlot& operator=(const EntrySlot&) = default; | |||
1143 | EntrySlot& operator=(EntrySlot&&) = default; | |||
1144 | ||||
1145 | bool operator==(const EntrySlot& aRhs) const { return mEntry == aRhs.mEntry; } | |||
1146 | ||||
1147 | bool operator<(const EntrySlot& aRhs) const { return mEntry < aRhs.mEntry; } | |||
1148 | ||||
1149 | EntrySlot& operator++() { | |||
1150 | ++mEntry; | |||
1151 | ++mKeyHash; | |||
1152 | return *this; | |||
1153 | } | |||
1154 | ||||
1155 | void destroy() { mEntry->destroy(); } | |||
1156 | ||||
1157 | void swap(EntrySlot& aOther) { | |||
1158 | mEntry->swap(aOther.mEntry, aOther.isLive()); | |||
1159 | std::swap(*mKeyHash, *aOther.mKeyHash); | |||
1160 | } | |||
1161 | ||||
1162 | T& get() const { return mEntry->get(); } | |||
1163 | ||||
1164 | NonConstT& getMutable() { return mEntry->getMutable(); } | |||
1165 | ||||
1166 | bool isFree() const { return *mKeyHash == Entry::sFreeKey; } | |||
1167 | ||||
1168 | void clearLive() { | |||
1169 | MOZ_ASSERT(isLive())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isLive())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isLive()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isLive()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1169); AnnotateMozCrashReason("MOZ_ASSERT" "(" "isLive()" ")" ); do { *((volatile int*)__null) = 1169; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1170 | *mKeyHash = Entry::sFreeKey; | |||
1171 | mEntry->destroyStoredT(); | |||
1172 | } | |||
1173 | ||||
1174 | void clear() { | |||
1175 | if (isLive()) { | |||
1176 | mEntry->destroyStoredT(); | |||
1177 | } | |||
1178 | MOZ_MAKE_MEM_UNDEFINED(mEntry, sizeof(*mEntry))do { } while (0); | |||
1179 | *mKeyHash = Entry::sFreeKey; | |||
1180 | } | |||
1181 | ||||
1182 | bool isRemoved() const { return *mKeyHash == Entry::sRemovedKey; } | |||
1183 | ||||
1184 | void removeLive() { | |||
1185 | MOZ_ASSERT(isLive())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isLive())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isLive()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isLive()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1185); AnnotateMozCrashReason("MOZ_ASSERT" "(" "isLive()" ")" ); do { *((volatile int*)__null) = 1185; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1186 | *mKeyHash = Entry::sRemovedKey; | |||
1187 | mEntry->destroyStoredT(); | |||
1188 | } | |||
1189 | ||||
1190 | bool isLive() const { return isLiveHash(*mKeyHash); } | |||
1191 | ||||
1192 | void setCollision() { | |||
1193 | MOZ_ASSERT(isLive())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isLive())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isLive()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isLive()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1193); AnnotateMozCrashReason("MOZ_ASSERT" "(" "isLive()" ")" ); do { *((volatile int*)__null) = 1193; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1194 | *mKeyHash |= Entry::sCollisionBit; | |||
1195 | } | |||
1196 | void unsetCollision() { *mKeyHash &= ~Entry::sCollisionBit; } | |||
1197 | bool hasCollision() const { return *mKeyHash & Entry::sCollisionBit; } | |||
1198 | bool matchHash(HashNumber hn) { | |||
1199 | return (*mKeyHash & ~Entry::sCollisionBit) == hn; | |||
1200 | } | |||
1201 | HashNumber getKeyHash() const { return *mKeyHash & ~Entry::sCollisionBit; } | |||
1202 | ||||
1203 | template <typename... Args> | |||
1204 | void setLive(HashNumber aHashNumber, Args&&... aArgs) { | |||
1205 | MOZ_ASSERT(!isLive())do { static_assert( mozilla::detail::AssertionConditionType< decltype(!isLive())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(!isLive()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("!isLive()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1205); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!isLive()" ")" ); do { *((volatile int*)__null) = 1205; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1206 | *mKeyHash = aHashNumber; | |||
1207 | new (KnownNotNull, mEntry->valuePtr()) T(std::forward<Args>(aArgs)...); | |||
1208 | MOZ_ASSERT(isLive())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isLive())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isLive()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isLive()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1208); AnnotateMozCrashReason("MOZ_ASSERT" "(" "isLive()" ")" ); do { *((volatile int*)__null) = 1208; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1209 | } | |||
1210 | ||||
1211 | Entry* toEntry() const { return mEntry; } | |||
1212 | }; | |||
1213 | ||||
1214 | template <class T, class HashPolicy, class AllocPolicy> | |||
1215 | class HashTable : private AllocPolicy { | |||
1216 | friend class mozilla::ReentrancyGuard; | |||
1217 | ||||
1218 | using NonConstT = std::remove_const_t<T>; | |||
1219 | using Key = typename HashPolicy::KeyType; | |||
1220 | using Lookup = typename HashPolicy::Lookup; | |||
1221 | ||||
1222 | public: | |||
1223 | using Entry = HashTableEntry<T>; | |||
1224 | using Slot = EntrySlot<T>; | |||
1225 | ||||
1226 | template <typename F> | |||
1227 | static void forEachSlot(char* aTable, uint32_t aCapacity, F&& f) { | |||
1228 | auto hashes = reinterpret_cast<HashNumber*>(aTable); | |||
1229 | auto entries = reinterpret_cast<Entry*>(&hashes[aCapacity]); | |||
1230 | Slot slot(entries, hashes); | |||
1231 | for (size_t i = 0; i < size_t(aCapacity); ++i) { | |||
1232 | f(slot); | |||
1233 | ++slot; | |||
1234 | } | |||
1235 | } | |||
1236 | ||||
1237 | // A nullable pointer to a hash table element. A Ptr |p| can be tested | |||
1238 | // either explicitly |if (p.found()) p->...| or using boolean conversion | |||
1239 | // |if (p) p->...|. Ptr objects must not be used after any mutating hash | |||
1240 | // table operations unless |generation()| is tested. | |||
1241 | class Ptr { | |||
1242 | friend class HashTable; | |||
1243 | ||||
1244 | Slot mSlot; | |||
1245 | #ifdef DEBUG1 | |||
1246 | const HashTable* mTable; | |||
1247 | Generation mGeneration; | |||
1248 | #endif | |||
1249 | ||||
1250 | protected: | |||
1251 | Ptr(Slot aSlot, const HashTable& aTable) | |||
1252 | : mSlot(aSlot) | |||
1253 | #ifdef DEBUG1 | |||
1254 | , | |||
1255 | mTable(&aTable), | |||
1256 | mGeneration(aTable.generation()) | |||
1257 | #endif | |||
1258 | { | |||
1259 | } | |||
1260 | ||||
1261 | // This constructor is used only by AddPtr() within lookupForAdd(). | |||
1262 | explicit Ptr(const HashTable& aTable) | |||
1263 | : mSlot(nullptr, nullptr) | |||
1264 | #ifdef DEBUG1 | |||
1265 | , | |||
1266 | mTable(&aTable), | |||
1267 | mGeneration(aTable.generation()) | |||
1268 | #endif | |||
1269 | { | |||
1270 | } | |||
1271 | ||||
1272 | bool isValid() const { return !!mSlot.toEntry(); } | |||
1273 | ||||
1274 | public: | |||
1275 | Ptr() | |||
1276 | : mSlot(nullptr, nullptr) | |||
1277 | #ifdef DEBUG1 | |||
1278 | , | |||
1279 | mTable(nullptr), | |||
1280 | mGeneration(0) | |||
1281 | #endif | |||
1282 | { | |||
1283 | } | |||
1284 | ||||
1285 | bool found() const { | |||
1286 | if (!isValid()) { | |||
1287 | return false; | |||
1288 | } | |||
1289 | #ifdef DEBUG1 | |||
1290 | MOZ_ASSERT(mGeneration == mTable->generation())do { static_assert( mozilla::detail::AssertionConditionType< decltype(mGeneration == mTable->generation())>::isValid , "invalid assertion condition"); if ((__builtin_expect(!!(!( !!(mGeneration == mTable->generation()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("mGeneration == mTable->generation()" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1290); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mGeneration == mTable->generation()" ")"); do { *((volatile int*)__null) = 1290; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1291 | #endif | |||
1292 | return mSlot.isLive(); | |||
1293 | } | |||
1294 | ||||
1295 | explicit operator bool() const { return found(); } | |||
1296 | ||||
1297 | bool operator==(const Ptr& aRhs) const { | |||
1298 | MOZ_ASSERT(found() && aRhs.found())do { static_assert( mozilla::detail::AssertionConditionType< decltype(found() && aRhs.found())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(found() && aRhs.found ()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("found() && aRhs.found()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1298); AnnotateMozCrashReason("MOZ_ASSERT" "(" "found() && aRhs.found()" ")"); do { *((volatile int*)__null) = 1298; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1299 | return mSlot == aRhs.mSlot; | |||
1300 | } | |||
1301 | ||||
1302 | bool operator!=(const Ptr& aRhs) const { | |||
1303 | #ifdef DEBUG1 | |||
1304 | MOZ_ASSERT(mGeneration == mTable->generation())do { static_assert( mozilla::detail::AssertionConditionType< decltype(mGeneration == mTable->generation())>::isValid , "invalid assertion condition"); if ((__builtin_expect(!!(!( !!(mGeneration == mTable->generation()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("mGeneration == mTable->generation()" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1304); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mGeneration == mTable->generation()" ")"); do { *((volatile int*)__null) = 1304; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1305 | #endif | |||
1306 | return !(*this == aRhs); | |||
1307 | } | |||
1308 | ||||
1309 | T& operator*() const { | |||
1310 | #ifdef DEBUG1 | |||
1311 | MOZ_ASSERT(found())do { static_assert( mozilla::detail::AssertionConditionType< decltype(found())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(found()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("found()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1311); AnnotateMozCrashReason("MOZ_ASSERT" "(" "found()" ")" ); do { *((volatile int*)__null) = 1311; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1312 | MOZ_ASSERT(mGeneration == mTable->generation())do { static_assert( mozilla::detail::AssertionConditionType< decltype(mGeneration == mTable->generation())>::isValid , "invalid assertion condition"); if ((__builtin_expect(!!(!( !!(mGeneration == mTable->generation()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("mGeneration == mTable->generation()" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1312); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mGeneration == mTable->generation()" ")"); do { *((volatile int*)__null) = 1312; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1313 | #endif | |||
1314 | return mSlot.get(); | |||
1315 | } | |||
1316 | ||||
1317 | T* operator->() const { | |||
1318 | #ifdef DEBUG1 | |||
1319 | MOZ_ASSERT(found())do { static_assert( mozilla::detail::AssertionConditionType< decltype(found())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(found()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("found()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1319); AnnotateMozCrashReason("MOZ_ASSERT" "(" "found()" ")" ); do { *((volatile int*)__null) = 1319; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1320 | MOZ_ASSERT(mGeneration == mTable->generation())do { static_assert( mozilla::detail::AssertionConditionType< decltype(mGeneration == mTable->generation())>::isValid , "invalid assertion condition"); if ((__builtin_expect(!!(!( !!(mGeneration == mTable->generation()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("mGeneration == mTable->generation()" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1320); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mGeneration == mTable->generation()" ")"); do { *((volatile int*)__null) = 1320; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| ||||
1321 | #endif | |||
1322 | return &mSlot.get(); | |||
1323 | } | |||
1324 | }; | |||
1325 | ||||
1326 | // A Ptr that can be used to add a key after a failed lookup. | |||
1327 | class AddPtr : public Ptr { | |||
1328 | friend class HashTable; | |||
1329 | ||||
1330 | HashNumber mKeyHash; | |||
1331 | #ifdef DEBUG1 | |||
1332 | uint64_t mMutationCount; | |||
1333 | #endif | |||
1334 | ||||
1335 | AddPtr(Slot aSlot, const HashTable& aTable, HashNumber aHashNumber) | |||
1336 | : Ptr(aSlot, aTable), | |||
1337 | mKeyHash(aHashNumber) | |||
1338 | #ifdef DEBUG1 | |||
1339 | , | |||
1340 | mMutationCount(aTable.mMutationCount) | |||
1341 | #endif | |||
1342 | { | |||
1343 | } | |||
1344 | ||||
1345 | // This constructor is used when lookupForAdd() is performed on a table | |||
1346 | // lacking entry storage; it leaves mSlot null but initializes everything | |||
1347 | // else. | |||
1348 | AddPtr(const HashTable& aTable, HashNumber aHashNumber) | |||
1349 | : Ptr(aTable), | |||
1350 | mKeyHash(aHashNumber) | |||
1351 | #ifdef DEBUG1 | |||
1352 | , | |||
1353 | mMutationCount(aTable.mMutationCount) | |||
1354 | #endif | |||
1355 | { | |||
1356 | MOZ_ASSERT(isLive())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isLive())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isLive()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isLive()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1356); AnnotateMozCrashReason("MOZ_ASSERT" "(" "isLive()" ")" ); do { *((volatile int*)__null) = 1356; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1357 | } | |||
1358 | ||||
1359 | bool isLive() const { return isLiveHash(mKeyHash); } | |||
1360 | ||||
1361 | public: | |||
1362 | AddPtr() : mKeyHash(0) {} | |||
1363 | }; | |||
1364 | ||||
1365 | // A hash table iterator that (mostly) doesn't allow table modifications. | |||
1366 | // As with Ptr/AddPtr, Iterator objects must not be used after any mutating | |||
1367 | // hash table operation unless the |generation()| is tested. | |||
1368 | class Iterator { | |||
1369 | void moveToNextLiveEntry() { | |||
1370 | while (++mCur < mEnd && !mCur.isLive()) { | |||
1371 | continue; | |||
1372 | } | |||
1373 | } | |||
1374 | ||||
1375 | protected: | |||
1376 | friend class HashTable; | |||
1377 | ||||
1378 | explicit Iterator(const HashTable& aTable) | |||
1379 | : mCur(aTable.slotForIndex(0)), | |||
1380 | mEnd(aTable.slotForIndex(aTable.capacity())) | |||
1381 | #ifdef DEBUG1 | |||
1382 | , | |||
1383 | mTable(aTable), | |||
1384 | mMutationCount(aTable.mMutationCount), | |||
1385 | mGeneration(aTable.generation()), | |||
1386 | mValidEntry(true) | |||
1387 | #endif | |||
1388 | { | |||
1389 | if (!done() && !mCur.isLive()) { | |||
1390 | moveToNextLiveEntry(); | |||
1391 | } | |||
1392 | } | |||
1393 | ||||
1394 | Slot mCur; | |||
1395 | Slot mEnd; | |||
1396 | #ifdef DEBUG1 | |||
1397 | const HashTable& mTable; | |||
1398 | uint64_t mMutationCount; | |||
1399 | Generation mGeneration; | |||
1400 | bool mValidEntry; | |||
1401 | #endif | |||
1402 | ||||
1403 | public: | |||
1404 | bool done() const { | |||
1405 | MOZ_ASSERT(mGeneration == mTable.generation())do { static_assert( mozilla::detail::AssertionConditionType< decltype(mGeneration == mTable.generation())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(mGeneration == mTable.generation ()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("mGeneration == mTable.generation()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1405); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mGeneration == mTable.generation()" ")"); do { *((volatile int*)__null) = 1405; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1406 | MOZ_ASSERT(mMutationCount == mTable.mMutationCount)do { static_assert( mozilla::detail::AssertionConditionType< decltype(mMutationCount == mTable.mMutationCount)>::isValid , "invalid assertion condition"); if ((__builtin_expect(!!(!( !!(mMutationCount == mTable.mMutationCount))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("mMutationCount == mTable.mMutationCount" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1406); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mMutationCount == mTable.mMutationCount" ")"); do { *((volatile int*)__null) = 1406; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1407 | return mCur == mEnd; | |||
1408 | } | |||
1409 | ||||
1410 | T& get() const { | |||
1411 | MOZ_ASSERT(!done())do { static_assert( mozilla::detail::AssertionConditionType< decltype(!done())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(!done()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("!done()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1411); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!done()" ")" ); do { *((volatile int*)__null) = 1411; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1412 | MOZ_ASSERT(mValidEntry)do { static_assert( mozilla::detail::AssertionConditionType< decltype(mValidEntry)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(mValidEntry))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("mValidEntry", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1412); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mValidEntry" ")"); do { *((volatile int*)__null) = 1412; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1413 | MOZ_ASSERT(mGeneration == mTable.generation())do { static_assert( mozilla::detail::AssertionConditionType< decltype(mGeneration == mTable.generation())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(mGeneration == mTable.generation ()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("mGeneration == mTable.generation()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1413); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mGeneration == mTable.generation()" ")"); do { *((volatile int*)__null) = 1413; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1414 | MOZ_ASSERT(mMutationCount == mTable.mMutationCount)do { static_assert( mozilla::detail::AssertionConditionType< decltype(mMutationCount == mTable.mMutationCount)>::isValid , "invalid assertion condition"); if ((__builtin_expect(!!(!( !!(mMutationCount == mTable.mMutationCount))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("mMutationCount == mTable.mMutationCount" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1414); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mMutationCount == mTable.mMutationCount" ")"); do { *((volatile int*)__null) = 1414; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1415 | return mCur.get(); | |||
1416 | } | |||
1417 | ||||
1418 | void next() { | |||
1419 | MOZ_ASSERT(!done())do { static_assert( mozilla::detail::AssertionConditionType< decltype(!done())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(!done()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("!done()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1419); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!done()" ")" ); do { *((volatile int*)__null) = 1419; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1420 | MOZ_ASSERT(mGeneration == mTable.generation())do { static_assert( mozilla::detail::AssertionConditionType< decltype(mGeneration == mTable.generation())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(mGeneration == mTable.generation ()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("mGeneration == mTable.generation()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1420); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mGeneration == mTable.generation()" ")"); do { *((volatile int*)__null) = 1420; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1421 | MOZ_ASSERT(mMutationCount == mTable.mMutationCount)do { static_assert( mozilla::detail::AssertionConditionType< decltype(mMutationCount == mTable.mMutationCount)>::isValid , "invalid assertion condition"); if ((__builtin_expect(!!(!( !!(mMutationCount == mTable.mMutationCount))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("mMutationCount == mTable.mMutationCount" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1421); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mMutationCount == mTable.mMutationCount" ")"); do { *((volatile int*)__null) = 1421; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1422 | moveToNextLiveEntry(); | |||
1423 | #ifdef DEBUG1 | |||
1424 | mValidEntry = true; | |||
1425 | #endif | |||
1426 | } | |||
1427 | }; | |||
1428 | ||||
1429 | // A hash table iterator that permits modification, removal and rekeying. | |||
1430 | // Since rehashing when elements were removed during enumeration would be | |||
1431 | // bad, it is postponed until the ModIterator is destructed. Since the | |||
1432 | // ModIterator's destructor touches the hash table, the user must ensure | |||
1433 | // that the hash table is still alive when the destructor runs. | |||
1434 | class ModIterator : public Iterator { | |||
1435 | friend class HashTable; | |||
1436 | ||||
1437 | HashTable& mTable; | |||
1438 | bool mRekeyed; | |||
1439 | bool mRemoved; | |||
1440 | ||||
1441 | // ModIterator is movable but not copyable. | |||
1442 | ModIterator(const ModIterator&) = delete; | |||
1443 | void operator=(const ModIterator&) = delete; | |||
1444 | ||||
1445 | protected: | |||
1446 | explicit ModIterator(HashTable& aTable) | |||
1447 | : Iterator(aTable), mTable(aTable), mRekeyed(false), mRemoved(false) {} | |||
1448 | ||||
1449 | public: | |||
1450 | MOZ_IMPLICIT ModIterator(ModIterator&& aOther) | |||
1451 | : Iterator(aOther), | |||
1452 | mTable(aOther.mTable), | |||
1453 | mRekeyed(aOther.mRekeyed), | |||
1454 | mRemoved(aOther.mRemoved) { | |||
1455 | aOther.mRekeyed = false; | |||
1456 | aOther.mRemoved = false; | |||
1457 | } | |||
1458 | ||||
1459 | // Removes the current element from the table, leaving |get()| | |||
1460 | // invalid until the next call to |next()|. | |||
1461 | void remove() { | |||
1462 | mTable.remove(this->mCur); | |||
1463 | mRemoved = true; | |||
1464 | #ifdef DEBUG1 | |||
1465 | this->mValidEntry = false; | |||
1466 | this->mMutationCount = mTable.mMutationCount; | |||
1467 | #endif | |||
1468 | } | |||
1469 | ||||
1470 | NonConstT& getMutable() { | |||
1471 | MOZ_ASSERT(!this->done())do { static_assert( mozilla::detail::AssertionConditionType< decltype(!this->done())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(!this->done()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("!this->done()" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1471); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!this->done()" ")"); do { *((volatile int*)__null) = 1471; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1472 | MOZ_ASSERT(this->mValidEntry)do { static_assert( mozilla::detail::AssertionConditionType< decltype(this->mValidEntry)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(this->mValidEntry))), 0)) ) { do { } while (false); MOZ_ReportAssertionFailure("this->mValidEntry" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1472); AnnotateMozCrashReason("MOZ_ASSERT" "(" "this->mValidEntry" ")"); do { *((volatile int*)__null) = 1472; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1473 | MOZ_ASSERT(this->mGeneration == this->Iterator::mTable.generation())do { static_assert( mozilla::detail::AssertionConditionType< decltype(this->mGeneration == this->Iterator::mTable.generation ())>::isValid, "invalid assertion condition"); if ((__builtin_expect (!!(!(!!(this->mGeneration == this->Iterator::mTable.generation ()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("this->mGeneration == this->Iterator::mTable.generation()" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1473); AnnotateMozCrashReason("MOZ_ASSERT" "(" "this->mGeneration == this->Iterator::mTable.generation()" ")"); do { *((volatile int*)__null) = 1473; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1474 | MOZ_ASSERT(this->mMutationCount == this->Iterator::mTable.mMutationCount)do { static_assert( mozilla::detail::AssertionConditionType< decltype(this->mMutationCount == this->Iterator::mTable .mMutationCount)>::isValid, "invalid assertion condition") ; if ((__builtin_expect(!!(!(!!(this->mMutationCount == this ->Iterator::mTable.mMutationCount))), 0))) { do { } while ( false); MOZ_ReportAssertionFailure("this->mMutationCount == this->Iterator::mTable.mMutationCount" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1474); AnnotateMozCrashReason("MOZ_ASSERT" "(" "this->mMutationCount == this->Iterator::mTable.mMutationCount" ")"); do { *((volatile int*)__null) = 1474; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1475 | return this->mCur.getMutable(); | |||
1476 | } | |||
1477 | ||||
1478 | // Removes the current element and re-inserts it into the table with | |||
1479 | // a new key at the new Lookup position. |get()| is invalid after | |||
1480 | // this operation until the next call to |next()|. | |||
1481 | void rekey(const Lookup& l, const Key& k) { | |||
1482 | MOZ_ASSERT(&k != &HashPolicy::getKey(this->mCur.get()))do { static_assert( mozilla::detail::AssertionConditionType< decltype(&k != &HashPolicy::getKey(this->mCur.get( )))>::isValid, "invalid assertion condition"); if ((__builtin_expect (!!(!(!!(&k != &HashPolicy::getKey(this->mCur.get( ))))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("&k != &HashPolicy::getKey(this->mCur.get())", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1482); AnnotateMozCrashReason("MOZ_ASSERT" "(" "&k != &HashPolicy::getKey(this->mCur.get())" ")"); do { *((volatile int*)__null) = 1482; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1483 | Ptr p(this->mCur, mTable); | |||
1484 | mTable.rekeyWithoutRehash(p, l, k); | |||
1485 | mRekeyed = true; | |||
1486 | #ifdef DEBUG1 | |||
1487 | this->mValidEntry = false; | |||
1488 | this->mMutationCount = mTable.mMutationCount; | |||
1489 | #endif | |||
1490 | } | |||
1491 | ||||
1492 | void rekey(const Key& k) { rekey(k, k); } | |||
1493 | ||||
1494 | // Potentially rehashes the table. | |||
1495 | ~ModIterator() { | |||
1496 | if (mRekeyed) { | |||
1497 | mTable.mGen++; | |||
1498 | mTable.infallibleRehashIfOverloaded(); | |||
1499 | } | |||
1500 | ||||
1501 | if (mRemoved) { | |||
1502 | mTable.compact(); | |||
1503 | } | |||
1504 | } | |||
1505 | }; | |||
1506 | ||||
1507 | // Range is similar to Iterator, but uses different terminology. | |||
1508 | class Range { | |||
1509 | friend class HashTable; | |||
1510 | ||||
1511 | Iterator mIter; | |||
1512 | ||||
1513 | protected: | |||
1514 | explicit Range(const HashTable& table) : mIter(table) {} | |||
1515 | ||||
1516 | public: | |||
1517 | bool empty() const { return mIter.done(); } | |||
1518 | ||||
1519 | T& front() const { return mIter.get(); } | |||
1520 | ||||
1521 | void popFront() { return mIter.next(); } | |||
1522 | }; | |||
1523 | ||||
1524 | // Enum is similar to ModIterator, but uses different terminology. | |||
1525 | class Enum { | |||
1526 | ModIterator mIter; | |||
1527 | ||||
1528 | // Enum is movable but not copyable. | |||
1529 | Enum(const Enum&) = delete; | |||
1530 | void operator=(const Enum&) = delete; | |||
1531 | ||||
1532 | public: | |||
1533 | template <class Map> | |||
1534 | explicit Enum(Map& map) : mIter(map.mImpl) {} | |||
1535 | ||||
1536 | MOZ_IMPLICIT Enum(Enum&& other) : mIter(std::move(other.mIter)) {} | |||
1537 | ||||
1538 | bool empty() const { return mIter.done(); } | |||
1539 | ||||
1540 | T& front() const { return mIter.get(); } | |||
1541 | ||||
1542 | void popFront() { return mIter.next(); } | |||
1543 | ||||
1544 | void removeFront() { mIter.remove(); } | |||
1545 | ||||
1546 | NonConstT& mutableFront() { return mIter.getMutable(); } | |||
1547 | ||||
1548 | void rekeyFront(const Lookup& aLookup, const Key& aKey) { | |||
1549 | mIter.rekey(aLookup, aKey); | |||
1550 | } | |||
1551 | ||||
1552 | void rekeyFront(const Key& aKey) { mIter.rekey(aKey); } | |||
1553 | }; | |||
1554 | ||||
1555 | // HashTable is movable | |||
1556 | HashTable(HashTable&& aRhs) : AllocPolicy(std::move(aRhs)) { moveFrom(aRhs); } | |||
1557 | HashTable& operator=(HashTable&& aRhs) { | |||
1558 | MOZ_ASSERT(this != &aRhs, "self-move assignment is prohibited")do { static_assert( mozilla::detail::AssertionConditionType< decltype(this != &aRhs)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(this != &aRhs))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("this != &aRhs" " (" "self-move assignment is prohibited" ")", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1558); AnnotateMozCrashReason("MOZ_ASSERT" "(" "this != &aRhs" ") (" "self-move assignment is prohibited" ")"); do { *((volatile int*)__null) = 1558; __attribute__((nomerge)) ::abort(); } while (false); } } while (false); | |||
1559 | if (mTable) { | |||
1560 | destroyTable(*this, mTable, capacity()); | |||
1561 | } | |||
1562 | AllocPolicy::operator=(std::move(aRhs)); | |||
1563 | moveFrom(aRhs); | |||
1564 | return *this; | |||
1565 | } | |||
1566 | ||||
1567 | void swap(HashTable& aOther) { | |||
1568 | ReentrancyGuard g1(*this); | |||
1569 | ReentrancyGuard g2(aOther); | |||
1570 | ||||
1571 | // Manual swap of generation because it's a bitfield | |||
1572 | uint64_t generation = mGen; | |||
1573 | mGen = aOther.mGen; | |||
1574 | aOther.mGen = generation; | |||
1575 | ||||
1576 | // Manual swap of hashShift because it's a bitfield | |||
1577 | uint64_t hashShift = mHashShift; | |||
1578 | mHashShift = aOther.mHashShift; | |||
1579 | aOther.mHashShift = hashShift; | |||
1580 | ||||
1581 | std::swap(mTable, aOther.mTable); | |||
1582 | std::swap(mEntryCount, aOther.mEntryCount); | |||
1583 | std::swap(mRemovedCount, aOther.mRemovedCount); | |||
1584 | #ifdef DEBUG1 | |||
1585 | std::swap(mMutationCount, aOther.mMutationCount); | |||
1586 | std::swap(mEntered, aOther.mEntered); | |||
1587 | #endif | |||
1588 | } | |||
1589 | ||||
1590 | private: | |||
1591 | void moveFrom(HashTable& aRhs) { | |||
1592 | mGen = aRhs.mGen; | |||
1593 | mHashShift = aRhs.mHashShift; | |||
1594 | mTable = aRhs.mTable; | |||
1595 | mEntryCount = aRhs.mEntryCount; | |||
1596 | mRemovedCount = aRhs.mRemovedCount; | |||
1597 | #ifdef DEBUG1 | |||
1598 | mMutationCount = aRhs.mMutationCount; | |||
1599 | mEntered = aRhs.mEntered; | |||
1600 | #endif | |||
1601 | aRhs.mTable = nullptr; | |||
1602 | aRhs.clearAndCompact(); | |||
1603 | } | |||
1604 | ||||
1605 | // HashTable is not copyable or assignable | |||
1606 | HashTable(const HashTable&) = delete; | |||
1607 | void operator=(const HashTable&) = delete; | |||
1608 | ||||
1609 | static const uint32_t CAP_BITS = 30; | |||
1610 | ||||
1611 | public: | |||
1612 | uint64_t mGen : 56; // entry storage generation number | |||
1613 | uint64_t mHashShift : 8; // multiplicative hash shift | |||
1614 | char* mTable; // entry storage | |||
1615 | uint32_t mEntryCount; // number of entries in mTable | |||
1616 | uint32_t mRemovedCount; // removed entry sentinels in mTable | |||
1617 | ||||
1618 | #ifdef DEBUG1 | |||
1619 | uint64_t mMutationCount; | |||
1620 | mutable bool mEntered; | |||
1621 | #endif | |||
1622 | ||||
1623 | // The default initial capacity is 32 (enough to hold 16 elements), but it | |||
1624 | // can be as low as 4. | |||
1625 | static const uint32_t sDefaultLen = 16; | |||
1626 | static const uint32_t sMinCapacity = 4; | |||
1627 | // See the comments in HashTableEntry about this value. | |||
1628 | static_assert(sMinCapacity >= 4, "too-small sMinCapacity breaks assumptions"); | |||
1629 | static const uint32_t sMaxInit = 1u << (CAP_BITS - 1); | |||
1630 | static const uint32_t sMaxCapacity = 1u << CAP_BITS; | |||
1631 | ||||
1632 | // Hash-table alpha is conceptually a fraction, but to avoid floating-point | |||
1633 | // math we implement it as a ratio of integers. | |||
1634 | static const uint8_t sAlphaDenominator = 4; | |||
1635 | static const uint8_t sMinAlphaNumerator = 1; // min alpha: 1/4 | |||
1636 | static const uint8_t sMaxAlphaNumerator = 3; // max alpha: 3/4 | |||
1637 | ||||
1638 | static const HashNumber sFreeKey = Entry::sFreeKey; | |||
1639 | static const HashNumber sRemovedKey = Entry::sRemovedKey; | |||
1640 | static const HashNumber sCollisionBit = Entry::sCollisionBit; | |||
1641 | ||||
1642 | static uint32_t bestCapacity(uint32_t aLen) { | |||
1643 | static_assert( | |||
1644 | (sMaxInit * sAlphaDenominator) / sAlphaDenominator == sMaxInit, | |||
1645 | "multiplication in numerator below could overflow"); | |||
1646 | static_assert( | |||
1647 | sMaxInit * sAlphaDenominator <= UINT32_MAX(4294967295U) - sMaxAlphaNumerator, | |||
1648 | "numerator calculation below could potentially overflow"); | |||
1649 | ||||
1650 | // Callers should ensure this is true. | |||
1651 | MOZ_ASSERT(aLen <= sMaxInit)do { static_assert( mozilla::detail::AssertionConditionType< decltype(aLen <= sMaxInit)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(aLen <= sMaxInit))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("aLen <= sMaxInit" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1651); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aLen <= sMaxInit" ")"); do { *((volatile int*)__null) = 1651; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1652 | ||||
1653 | // Compute the smallest capacity allowing |aLen| elements to be | |||
1654 | // inserted without rehashing: ceil(aLen / max-alpha). (Ceiling | |||
1655 | // integral division: <http://stackoverflow.com/a/2745086>.) | |||
1656 | uint32_t capacity = (aLen * sAlphaDenominator + sMaxAlphaNumerator - 1) / | |||
1657 | sMaxAlphaNumerator; | |||
1658 | capacity = (capacity < sMinCapacity) ? sMinCapacity : RoundUpPow2(capacity); | |||
1659 | ||||
1660 | MOZ_ASSERT(capacity >= aLen)do { static_assert( mozilla::detail::AssertionConditionType< decltype(capacity >= aLen)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(capacity >= aLen))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("capacity >= aLen" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1660); AnnotateMozCrashReason("MOZ_ASSERT" "(" "capacity >= aLen" ")"); do { *((volatile int*)__null) = 1660; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1661 | MOZ_ASSERT(capacity <= sMaxCapacity)do { static_assert( mozilla::detail::AssertionConditionType< decltype(capacity <= sMaxCapacity)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(capacity <= sMaxCapacity) )), 0))) { do { } while (false); MOZ_ReportAssertionFailure("capacity <= sMaxCapacity" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1661); AnnotateMozCrashReason("MOZ_ASSERT" "(" "capacity <= sMaxCapacity" ")"); do { *((volatile int*)__null) = 1661; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1662 | ||||
1663 | return capacity; | |||
1664 | } | |||
1665 | ||||
1666 | static uint32_t hashShift(uint32_t aLen) { | |||
1667 | // Reject all lengths whose initial computed capacity would exceed | |||
1668 | // sMaxCapacity. Round that maximum aLen down to the nearest power of two | |||
1669 | // for speedier code. | |||
1670 | if (MOZ_UNLIKELY(aLen > sMaxInit)(__builtin_expect(!!(aLen > sMaxInit), 0))) { | |||
1671 | MOZ_CRASH("initial length is too large")do { do { } while (false); MOZ_ReportCrash("" "initial length is too large" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1671); AnnotateMozCrashReason("MOZ_CRASH(" "initial length is too large" ")"); do { *((volatile int*)__null) = 1671; __attribute__((nomerge )) ::abort(); } while (false); } while (false); | |||
1672 | } | |||
1673 | ||||
1674 | return kHashNumberBits - mozilla::CeilingLog2(bestCapacity(aLen)); | |||
1675 | } | |||
1676 | ||||
1677 | static bool isLiveHash(HashNumber aHash) { return Entry::isLiveHash(aHash); } | |||
1678 | ||||
1679 | static HashNumber prepareHash(HashNumber aInputHash) { | |||
1680 | HashNumber keyHash = ScrambleHashCode(aInputHash); | |||
1681 | ||||
1682 | // Avoid reserved hash codes. | |||
1683 | if (!isLiveHash(keyHash)) { | |||
1684 | keyHash -= (sRemovedKey + 1); | |||
1685 | } | |||
1686 | return keyHash & ~sCollisionBit; | |||
1687 | } | |||
1688 | ||||
1689 | enum FailureBehavior { DontReportFailure = false, ReportFailure = true }; | |||
1690 | ||||
1691 | // Fake a struct that we're going to alloc. See the comments in | |||
1692 | // HashTableEntry about how the table is laid out, and why it's safe. | |||
1693 | struct FakeSlot { | |||
1694 | unsigned char c[sizeof(HashNumber) + sizeof(typename Entry::NonConstT)]; | |||
1695 | }; | |||
1696 | ||||
1697 | static char* createTable(AllocPolicy& aAllocPolicy, uint32_t aCapacity, | |||
1698 | FailureBehavior aReportFailure = ReportFailure) { | |||
1699 | FakeSlot* fake = | |||
1700 | aReportFailure | |||
1701 | ? aAllocPolicy.template pod_malloc<FakeSlot>(aCapacity) | |||
1702 | : aAllocPolicy.template maybe_pod_malloc<FakeSlot>(aCapacity); | |||
1703 | ||||
1704 | MOZ_ASSERT((reinterpret_cast<uintptr_t>(fake) % Entry::kMinimumAlignment) ==do { static_assert( mozilla::detail::AssertionConditionType< decltype((reinterpret_cast<uintptr_t>(fake) % Entry::kMinimumAlignment ) == 0)>::isValid, "invalid assertion condition"); if ((__builtin_expect (!!(!(!!((reinterpret_cast<uintptr_t>(fake) % Entry::kMinimumAlignment ) == 0))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("(reinterpret_cast<uintptr_t>(fake) % Entry::kMinimumAlignment) == 0" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1705); AnnotateMozCrashReason("MOZ_ASSERT" "(" "(reinterpret_cast<uintptr_t>(fake) % Entry::kMinimumAlignment) == 0" ")"); do { *((volatile int*)__null) = 1705; __attribute__((nomerge )) ::abort(); } while (false); } } while (false) | |||
1705 | 0)do { static_assert( mozilla::detail::AssertionConditionType< decltype((reinterpret_cast<uintptr_t>(fake) % Entry::kMinimumAlignment ) == 0)>::isValid, "invalid assertion condition"); if ((__builtin_expect (!!(!(!!((reinterpret_cast<uintptr_t>(fake) % Entry::kMinimumAlignment ) == 0))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("(reinterpret_cast<uintptr_t>(fake) % Entry::kMinimumAlignment) == 0" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1705); AnnotateMozCrashReason("MOZ_ASSERT" "(" "(reinterpret_cast<uintptr_t>(fake) % Entry::kMinimumAlignment) == 0" ")"); do { *((volatile int*)__null) = 1705; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1706 | ||||
1707 | char* table = reinterpret_cast<char*>(fake); | |||
1708 | if (table) { | |||
1709 | forEachSlot(table, aCapacity, [&](Slot& slot) { | |||
1710 | *slot.mKeyHash = sFreeKey; | |||
1711 | new (KnownNotNull, slot.toEntry()) Entry(); | |||
1712 | }); | |||
1713 | } | |||
1714 | return table; | |||
1715 | } | |||
1716 | ||||
1717 | static void destroyTable(AllocPolicy& aAllocPolicy, char* aOldTable, | |||
1718 | uint32_t aCapacity) { | |||
1719 | forEachSlot(aOldTable, aCapacity, [&](const Slot& slot) { | |||
1720 | if (slot.isLive()) { | |||
1721 | slot.toEntry()->destroyStoredT(); | |||
1722 | } | |||
1723 | }); | |||
1724 | freeTable(aAllocPolicy, aOldTable, aCapacity); | |||
1725 | } | |||
1726 | ||||
1727 | static void freeTable(AllocPolicy& aAllocPolicy, char* aOldTable, | |||
1728 | uint32_t aCapacity) { | |||
1729 | FakeSlot* fake = reinterpret_cast<FakeSlot*>(aOldTable); | |||
1730 | aAllocPolicy.free_(fake, aCapacity); | |||
1731 | } | |||
1732 | ||||
1733 | public: | |||
1734 | HashTable(AllocPolicy aAllocPolicy, uint32_t aLen) | |||
1735 | : AllocPolicy(std::move(aAllocPolicy)), | |||
1736 | mGen(0), | |||
1737 | mHashShift(hashShift(aLen)), | |||
1738 | mTable(nullptr), | |||
1739 | mEntryCount(0), | |||
1740 | mRemovedCount(0) | |||
1741 | #ifdef DEBUG1 | |||
1742 | , | |||
1743 | mMutationCount(0), | |||
1744 | mEntered(false) | |||
1745 | #endif | |||
1746 | { | |||
1747 | } | |||
1748 | ||||
1749 | explicit HashTable(AllocPolicy aAllocPolicy) | |||
1750 | : HashTable(aAllocPolicy, sDefaultLen) {} | |||
1751 | ||||
1752 | ~HashTable() { | |||
1753 | if (mTable) { | |||
1754 | destroyTable(*this, mTable, capacity()); | |||
1755 | } | |||
1756 | } | |||
1757 | ||||
1758 | private: | |||
1759 | HashNumber hash1(HashNumber aHash0) const { return aHash0 >> mHashShift; } | |||
1760 | ||||
1761 | struct DoubleHash { | |||
1762 | HashNumber mHash2; | |||
1763 | HashNumber mSizeMask; | |||
1764 | }; | |||
1765 | ||||
1766 | DoubleHash hash2(HashNumber aCurKeyHash) const { | |||
1767 | uint32_t sizeLog2 = kHashNumberBits - mHashShift; | |||
1768 | DoubleHash dh = {((aCurKeyHash << sizeLog2) >> mHashShift) | 1, | |||
1769 | (HashNumber(1) << sizeLog2) - 1}; | |||
1770 | return dh; | |||
1771 | } | |||
1772 | ||||
1773 | static HashNumber applyDoubleHash(HashNumber aHash1, | |||
1774 | const DoubleHash& aDoubleHash) { | |||
1775 | return WrappingSubtract(aHash1, aDoubleHash.mHash2) & aDoubleHash.mSizeMask; | |||
1776 | } | |||
1777 | ||||
1778 | static MOZ_ALWAYS_INLINEinline bool match(T& aEntry, const Lookup& aLookup) { | |||
1779 | return HashPolicy::match(HashPolicy::getKey(aEntry), aLookup); | |||
1780 | } | |||
1781 | ||||
1782 | enum LookupReason { ForNonAdd, ForAdd }; | |||
1783 | ||||
1784 | Slot slotForIndex(HashNumber aIndex) const { | |||
1785 | auto hashes = reinterpret_cast<HashNumber*>(mTable); | |||
1786 | auto entries = reinterpret_cast<Entry*>(&hashes[capacity()]); | |||
1787 | return Slot(&entries[aIndex], &hashes[aIndex]); | |||
1788 | } | |||
1789 | ||||
1790 | // Warning: in order for readonlyThreadsafeLookup() to be safe this | |||
1791 | // function must not modify the table in any way when Reason==ForNonAdd. | |||
1792 | template <LookupReason Reason> | |||
1793 | MOZ_ALWAYS_INLINEinline Slot lookup(const Lookup& aLookup, | |||
1794 | HashNumber aKeyHash) const { | |||
1795 | MOZ_ASSERT(isLiveHash(aKeyHash))do { static_assert( mozilla::detail::AssertionConditionType< decltype(isLiveHash(aKeyHash))>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isLiveHash(aKeyHash)))), 0)) ) { do { } while (false); MOZ_ReportAssertionFailure("isLiveHash(aKeyHash)" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1795); AnnotateMozCrashReason("MOZ_ASSERT" "(" "isLiveHash(aKeyHash)" ")"); do { *((volatile int*)__null) = 1795; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1796 | MOZ_ASSERT(!(aKeyHash & sCollisionBit))do { static_assert( mozilla::detail::AssertionConditionType< decltype(!(aKeyHash & sCollisionBit))>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(!(aKeyHash & sCollisionBit )))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("!(aKeyHash & sCollisionBit)", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1796); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!(aKeyHash & sCollisionBit)" ")"); do { *((volatile int*)__null) = 1796; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1797 | MOZ_ASSERT(mTable)do { static_assert( mozilla::detail::AssertionConditionType< decltype(mTable)>::isValid, "invalid assertion condition") ; if ((__builtin_expect(!!(!(!!(mTable))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("mTable", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1797); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mTable" ")" ); do { *((volatile int*)__null) = 1797; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1798 | ||||
1799 | // Compute the primary hash address. | |||
1800 | HashNumber h1 = hash1(aKeyHash); | |||
1801 | Slot slot = slotForIndex(h1); | |||
1802 | ||||
1803 | // Miss: return space for a new entry. | |||
1804 | if (slot.isFree()) { | |||
1805 | return slot; | |||
1806 | } | |||
1807 | ||||
1808 | // Hit: return entry. | |||
1809 | if (slot.matchHash(aKeyHash) && match(slot.get(), aLookup)) { | |||
1810 | return slot; | |||
1811 | } | |||
1812 | ||||
1813 | // Collision: double hash. | |||
1814 | DoubleHash dh = hash2(aKeyHash); | |||
1815 | ||||
1816 | // Save the first removed entry pointer so we can recycle later. | |||
1817 | Maybe<Slot> firstRemoved; | |||
1818 | ||||
1819 | while (true) { | |||
1820 | if (Reason == ForAdd && !firstRemoved) { | |||
1821 | if (MOZ_UNLIKELY(slot.isRemoved())(__builtin_expect(!!(slot.isRemoved()), 0))) { | |||
1822 | firstRemoved.emplace(slot); | |||
1823 | } else { | |||
1824 | slot.setCollision(); | |||
1825 | } | |||
1826 | } | |||
1827 | ||||
1828 | h1 = applyDoubleHash(h1, dh); | |||
1829 | ||||
1830 | slot = slotForIndex(h1); | |||
1831 | if (slot.isFree()) { | |||
1832 | return firstRemoved.refOr(slot); | |||
1833 | } | |||
1834 | ||||
1835 | if (slot.matchHash(aKeyHash) && match(slot.get(), aLookup)) { | |||
1836 | return slot; | |||
1837 | } | |||
1838 | } | |||
1839 | } | |||
1840 | ||||
1841 | // This is a copy of lookup() hardcoded to the assumptions: | |||
1842 | // 1. the lookup is for an add; | |||
1843 | // 2. the key, whose |keyHash| has been passed, is not in the table. | |||
1844 | Slot findNonLiveSlot(HashNumber aKeyHash) { | |||
1845 | MOZ_ASSERT(!(aKeyHash & sCollisionBit))do { static_assert( mozilla::detail::AssertionConditionType< decltype(!(aKeyHash & sCollisionBit))>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(!(aKeyHash & sCollisionBit )))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("!(aKeyHash & sCollisionBit)", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1845); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!(aKeyHash & sCollisionBit)" ")"); do { *((volatile int*)__null) = 1845; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1846 | MOZ_ASSERT(mTable)do { static_assert( mozilla::detail::AssertionConditionType< decltype(mTable)>::isValid, "invalid assertion condition") ; if ((__builtin_expect(!!(!(!!(mTable))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("mTable", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1846); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mTable" ")" ); do { *((volatile int*)__null) = 1846; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1847 | ||||
1848 | // We assume 'aKeyHash' has already been distributed. | |||
1849 | ||||
1850 | // Compute the primary hash address. | |||
1851 | HashNumber h1 = hash1(aKeyHash); | |||
1852 | Slot slot = slotForIndex(h1); | |||
1853 | ||||
1854 | // Miss: return space for a new entry. | |||
1855 | if (!slot.isLive()) { | |||
1856 | return slot; | |||
1857 | } | |||
1858 | ||||
1859 | // Collision: double hash. | |||
1860 | DoubleHash dh = hash2(aKeyHash); | |||
1861 | ||||
1862 | while (true) { | |||
1863 | slot.setCollision(); | |||
1864 | ||||
1865 | h1 = applyDoubleHash(h1, dh); | |||
1866 | ||||
1867 | slot = slotForIndex(h1); | |||
1868 | if (!slot.isLive()) { | |||
1869 | return slot; | |||
1870 | } | |||
1871 | } | |||
1872 | } | |||
1873 | ||||
1874 | enum RebuildStatus { NotOverloaded, Rehashed, RehashFailed }; | |||
1875 | ||||
1876 | RebuildStatus changeTableSize( | |||
1877 | uint32_t newCapacity, FailureBehavior aReportFailure = ReportFailure) { | |||
1878 | MOZ_ASSERT(IsPowerOfTwo(newCapacity))do { static_assert( mozilla::detail::AssertionConditionType< decltype(IsPowerOfTwo(newCapacity))>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(IsPowerOfTwo(newCapacity)))) , 0))) { do { } while (false); MOZ_ReportAssertionFailure("IsPowerOfTwo(newCapacity)" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1878); AnnotateMozCrashReason("MOZ_ASSERT" "(" "IsPowerOfTwo(newCapacity)" ")"); do { *((volatile int*)__null) = 1878; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1879 | MOZ_ASSERT(!!mTable == !!capacity())do { static_assert( mozilla::detail::AssertionConditionType< decltype(!!mTable == !!capacity())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(!!mTable == !!capacity()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("!!mTable == !!capacity()" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1879); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!!mTable == !!capacity()" ")"); do { *((volatile int*)__null) = 1879; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1880 | ||||
1881 | // Look, but don't touch, until we succeed in getting new entry store. | |||
1882 | char* oldTable = mTable; | |||
1883 | uint32_t oldCapacity = capacity(); | |||
1884 | uint32_t newLog2 = mozilla::CeilingLog2(newCapacity); | |||
1885 | ||||
1886 | if (MOZ_UNLIKELY(newCapacity > sMaxCapacity)(__builtin_expect(!!(newCapacity > sMaxCapacity), 0))) { | |||
1887 | if (aReportFailure) { | |||
1888 | this->reportAllocOverflow(); | |||
1889 | } | |||
1890 | return RehashFailed; | |||
1891 | } | |||
1892 | ||||
1893 | char* newTable = createTable(*this, newCapacity, aReportFailure); | |||
1894 | if (!newTable) { | |||
1895 | return RehashFailed; | |||
1896 | } | |||
1897 | ||||
1898 | // We can't fail from here on, so update table parameters. | |||
1899 | mHashShift = kHashNumberBits - newLog2; | |||
1900 | mRemovedCount = 0; | |||
1901 | mGen++; | |||
1902 | mTable = newTable; | |||
1903 | ||||
1904 | // Copy only live entries, leaving removed ones behind. | |||
1905 | forEachSlot(oldTable, oldCapacity, [&](Slot& slot) { | |||
1906 | if (slot.isLive()) { | |||
1907 | HashNumber hn = slot.getKeyHash(); | |||
1908 | findNonLiveSlot(hn).setLive( | |||
1909 | hn, std::move(const_cast<typename Entry::NonConstT&>(slot.get()))); | |||
1910 | } | |||
1911 | ||||
1912 | slot.clear(); | |||
1913 | }); | |||
1914 | ||||
1915 | // All entries have been destroyed, no need to destroyTable. | |||
1916 | freeTable(*this, oldTable, oldCapacity); | |||
1917 | return Rehashed; | |||
1918 | } | |||
1919 | ||||
1920 | RebuildStatus rehashIfOverloaded( | |||
1921 | FailureBehavior aReportFailure = ReportFailure) { | |||
1922 | static_assert(sMaxCapacity <= UINT32_MAX(4294967295U) / sMaxAlphaNumerator, | |||
1923 | "multiplication below could overflow"); | |||
1924 | ||||
1925 | // Note: if capacity() is zero, this will always succeed, which is | |||
1926 | // what we want. | |||
1927 | bool overloaded = mEntryCount + mRemovedCount >= | |||
1928 | capacity() * sMaxAlphaNumerator / sAlphaDenominator; | |||
1929 | ||||
1930 | if (!overloaded) { | |||
1931 | return NotOverloaded; | |||
1932 | } | |||
1933 | ||||
1934 | // Succeed if a quarter or more of all entries are removed. Note that this | |||
1935 | // always succeeds if capacity() == 0 (i.e. entry storage has not been | |||
1936 | // allocated), which is what we want, because it means changeTableSize() | |||
1937 | // will allocate the requested capacity rather than doubling it. | |||
1938 | bool manyRemoved = mRemovedCount >= (capacity() >> 2); | |||
1939 | uint32_t newCapacity = manyRemoved ? rawCapacity() : rawCapacity() * 2; | |||
1940 | return changeTableSize(newCapacity, aReportFailure); | |||
1941 | } | |||
1942 | ||||
1943 | void infallibleRehashIfOverloaded() { | |||
1944 | if (rehashIfOverloaded(DontReportFailure) == RehashFailed) { | |||
1945 | rehashTableInPlace(); | |||
1946 | } | |||
1947 | } | |||
1948 | ||||
1949 | void remove(Slot& aSlot) { | |||
1950 | MOZ_ASSERT(mTable)do { static_assert( mozilla::detail::AssertionConditionType< decltype(mTable)>::isValid, "invalid assertion condition") ; if ((__builtin_expect(!!(!(!!(mTable))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("mTable", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1950); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mTable" ")" ); do { *((volatile int*)__null) = 1950; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1951 | ||||
1952 | if (aSlot.hasCollision()) { | |||
1953 | aSlot.removeLive(); | |||
1954 | mRemovedCount++; | |||
1955 | } else { | |||
1956 | aSlot.clearLive(); | |||
1957 | } | |||
1958 | mEntryCount--; | |||
1959 | #ifdef DEBUG1 | |||
1960 | mMutationCount++; | |||
1961 | #endif | |||
1962 | } | |||
1963 | ||||
1964 | void shrinkIfUnderloaded() { | |||
1965 | static_assert(sMaxCapacity <= UINT32_MAX(4294967295U) / sMinAlphaNumerator, | |||
1966 | "multiplication below could overflow"); | |||
1967 | bool underloaded = | |||
1968 | capacity() > sMinCapacity && | |||
1969 | mEntryCount <= capacity() * sMinAlphaNumerator / sAlphaDenominator; | |||
1970 | ||||
1971 | if (underloaded) { | |||
1972 | (void)changeTableSize(capacity() / 2, DontReportFailure); | |||
1973 | } | |||
1974 | } | |||
1975 | ||||
1976 | // This is identical to changeTableSize(currentSize), but without requiring | |||
1977 | // a second table. We do this by recycling the collision bits to tell us if | |||
1978 | // the element is already inserted or still waiting to be inserted. Since | |||
1979 | // already-inserted elements win any conflicts, we get the same table as we | |||
1980 | // would have gotten through random insertion order. | |||
1981 | void rehashTableInPlace() { | |||
1982 | mRemovedCount = 0; | |||
1983 | mGen++; | |||
1984 | forEachSlot(mTable, capacity(), [&](Slot& slot) { slot.unsetCollision(); }); | |||
1985 | for (uint32_t i = 0; i < capacity();) { | |||
1986 | Slot src = slotForIndex(i); | |||
1987 | ||||
1988 | if (!src.isLive() || src.hasCollision()) { | |||
1989 | ++i; | |||
1990 | continue; | |||
1991 | } | |||
1992 | ||||
1993 | HashNumber keyHash = src.getKeyHash(); | |||
1994 | HashNumber h1 = hash1(keyHash); | |||
1995 | DoubleHash dh = hash2(keyHash); | |||
1996 | Slot tgt = slotForIndex(h1); | |||
1997 | while (true) { | |||
1998 | if (!tgt.hasCollision()) { | |||
1999 | src.swap(tgt); | |||
2000 | tgt.setCollision(); | |||
2001 | break; | |||
2002 | } | |||
2003 | ||||
2004 | h1 = applyDoubleHash(h1, dh); | |||
2005 | tgt = slotForIndex(h1); | |||
2006 | } | |||
2007 | } | |||
2008 | ||||
2009 | // TODO: this algorithm leaves collision bits on *all* elements, even if | |||
2010 | // they are on no collision path. We have the option of setting the | |||
2011 | // collision bits correctly on a subsequent pass or skipping the rehash | |||
2012 | // unless we are totally filled with tombstones: benchmark to find out | |||
2013 | // which approach is best. | |||
2014 | } | |||
2015 | ||||
2016 | // Prefer to use putNewInfallible; this function does not check | |||
2017 | // invariants. | |||
2018 | template <typename... Args> | |||
2019 | void putNewInfallibleInternal(HashNumber aKeyHash, Args&&... aArgs) { | |||
2020 | MOZ_ASSERT(mTable)do { static_assert( mozilla::detail::AssertionConditionType< decltype(mTable)>::isValid, "invalid assertion condition") ; if ((__builtin_expect(!!(!(!!(mTable))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("mTable", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2020); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mTable" ")" ); do { *((volatile int*)__null) = 2020; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
2021 | ||||
2022 | Slot slot = findNonLiveSlot(aKeyHash); | |||
2023 | ||||
2024 | if (slot.isRemoved()) { | |||
2025 | mRemovedCount--; | |||
2026 | aKeyHash |= sCollisionBit; | |||
2027 | } | |||
2028 | ||||
2029 | slot.setLive(aKeyHash, std::forward<Args>(aArgs)...); | |||
2030 | mEntryCount++; | |||
2031 | #ifdef DEBUG1 | |||
2032 | mMutationCount++; | |||
2033 | #endif | |||
2034 | } | |||
2035 | ||||
2036 | public: | |||
2037 | void clear() { | |||
2038 | forEachSlot(mTable, capacity(), [&](Slot& slot) { slot.clear(); }); | |||
2039 | mRemovedCount = 0; | |||
2040 | mEntryCount = 0; | |||
2041 | #ifdef DEBUG1 | |||
2042 | mMutationCount++; | |||
2043 | #endif | |||
2044 | } | |||
2045 | ||||
2046 | // Resize the table down to the smallest capacity that doesn't overload the | |||
2047 | // table. Since we call shrinkIfUnderloaded() on every remove, you only need | |||
2048 | // to call this after a bulk removal of items done without calling remove(). | |||
2049 | void compact() { | |||
2050 | if (empty()) { | |||
2051 | // Free the entry storage. | |||
2052 | freeTable(*this, mTable, capacity()); | |||
2053 | mGen++; | |||
2054 | mHashShift = hashShift(0); // gives minimum capacity on regrowth | |||
2055 | mTable = nullptr; | |||
2056 | mRemovedCount = 0; | |||
2057 | return; | |||
2058 | } | |||
2059 | ||||
2060 | uint32_t bestCapacity = this->bestCapacity(mEntryCount); | |||
2061 | MOZ_ASSERT(bestCapacity <= capacity())do { static_assert( mozilla::detail::AssertionConditionType< decltype(bestCapacity <= capacity())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(bestCapacity <= capacity( )))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("bestCapacity <= capacity()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2061); AnnotateMozCrashReason("MOZ_ASSERT" "(" "bestCapacity <= capacity()" ")"); do { *((volatile int*)__null) = 2061; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
2062 | ||||
2063 | if (bestCapacity < capacity()) { | |||
2064 | (void)changeTableSize(bestCapacity, DontReportFailure); | |||
2065 | } | |||
2066 | } | |||
2067 | ||||
2068 | void clearAndCompact() { | |||
2069 | clear(); | |||
2070 | compact(); | |||
2071 | } | |||
2072 | ||||
2073 | [[nodiscard]] bool reserve(uint32_t aLen) { | |||
2074 | if (aLen == 0) { | |||
2075 | return true; | |||
2076 | } | |||
2077 | ||||
2078 | if (MOZ_UNLIKELY(aLen > sMaxInit)(__builtin_expect(!!(aLen > sMaxInit), 0))) { | |||
2079 | this->reportAllocOverflow(); | |||
2080 | return false; | |||
2081 | } | |||
2082 | ||||
2083 | uint32_t bestCapacity = this->bestCapacity(aLen); | |||
2084 | if (bestCapacity <= capacity()) { | |||
2085 | return true; // Capacity is already sufficient. | |||
2086 | } | |||
2087 | ||||
2088 | RebuildStatus status = changeTableSize(bestCapacity, ReportFailure); | |||
2089 | MOZ_ASSERT(status != NotOverloaded)do { static_assert( mozilla::detail::AssertionConditionType< decltype(status != NotOverloaded)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(status != NotOverloaded))), 0 ))) { do { } while (false); MOZ_ReportAssertionFailure("status != NotOverloaded" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2089); AnnotateMozCrashReason("MOZ_ASSERT" "(" "status != NotOverloaded" ")"); do { *((volatile int*)__null) = 2089; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
2090 | return status != RehashFailed; | |||
2091 | } | |||
2092 | ||||
2093 | Iterator iter() const { return Iterator(*this); } | |||
2094 | ||||
2095 | ModIterator modIter() { return ModIterator(*this); } | |||
2096 | ||||
2097 | Range all() const { return Range(*this); } | |||
2098 | ||||
2099 | bool empty() const { return mEntryCount == 0; } | |||
2100 | ||||
2101 | uint32_t count() const { return mEntryCount; } | |||
2102 | ||||
2103 | uint32_t rawCapacity() const { return 1u << (kHashNumberBits - mHashShift); } | |||
2104 | ||||
2105 | uint32_t capacity() const { return mTable ? rawCapacity() : 0; } | |||
2106 | ||||
2107 | Generation generation() const { return Generation(mGen); } | |||
2108 | ||||
2109 | size_t shallowSizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const { | |||
2110 | return aMallocSizeOf(mTable); | |||
2111 | } | |||
2112 | ||||
2113 | size_t shallowSizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const { | |||
2114 | return aMallocSizeOf(this) + shallowSizeOfExcludingThis(aMallocSizeOf); | |||
2115 | } | |||
2116 | ||||
2117 | MOZ_ALWAYS_INLINEinline Ptr readonlyThreadsafeLookup(const Lookup& aLookup) const { | |||
2118 | if (empty()) { | |||
2119 | return Ptr(); | |||
2120 | } | |||
2121 | ||||
2122 | HashNumber inputHash; | |||
2123 | if (!MaybeGetHash<HashPolicy>(aLookup, &inputHash)) { | |||
2124 | return Ptr(); | |||
2125 | } | |||
2126 | ||||
2127 | HashNumber keyHash = prepareHash(inputHash); | |||
2128 | return Ptr(lookup<ForNonAdd>(aLookup, keyHash), *this); | |||
2129 | } | |||
2130 | ||||
2131 | MOZ_ALWAYS_INLINEinline Ptr lookup(const Lookup& aLookup) const { | |||
2132 | ReentrancyGuard g(*this); | |||
2133 | return readonlyThreadsafeLookup(aLookup); | |||
2134 | } | |||
2135 | ||||
2136 | MOZ_ALWAYS_INLINEinline AddPtr lookupForAdd(const Lookup& aLookup) { | |||
2137 | ReentrancyGuard g(*this); | |||
2138 | ||||
2139 | HashNumber inputHash; | |||
2140 | if (!EnsureHash<HashPolicy>(aLookup, &inputHash)) { | |||
2141 | return AddPtr(); | |||
2142 | } | |||
2143 | ||||
2144 | HashNumber keyHash = prepareHash(inputHash); | |||
2145 | ||||
2146 | if (!mTable) { | |||
2147 | return AddPtr(*this, keyHash); | |||
2148 | } | |||
2149 | ||||
2150 | // Directly call the constructor in the return statement to avoid | |||
2151 | // excess copying when building with Visual Studio 2017. | |||
2152 | // See bug 1385181. | |||
2153 | return AddPtr(lookup<ForAdd>(aLookup, keyHash), *this, keyHash); | |||
2154 | } | |||
2155 | ||||
2156 | template <typename... Args> | |||
2157 | [[nodiscard]] bool add(AddPtr& aPtr, Args&&... aArgs) { | |||
2158 | ReentrancyGuard g(*this); | |||
2159 | MOZ_ASSERT_IF(aPtr.isValid(), mTable)do { if (aPtr.isValid()) { do { static_assert( mozilla::detail ::AssertionConditionType<decltype(mTable)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(mTable))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("mTable", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2159); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mTable" ")" ); do { *((volatile int*)__null) = 2159; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); } } while ( false); | |||
2160 | MOZ_ASSERT_IF(aPtr.isValid(), aPtr.mTable == this)do { if (aPtr.isValid()) { do { static_assert( mozilla::detail ::AssertionConditionType<decltype(aPtr.mTable == this)> ::isValid, "invalid assertion condition"); if ((__builtin_expect (!!(!(!!(aPtr.mTable == this))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("aPtr.mTable == this", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2160); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPtr.mTable == this" ")"); do { *((volatile int*)__null) = 2160; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); } } while ( false); | |||
2161 | MOZ_ASSERT(!aPtr.found())do { static_assert( mozilla::detail::AssertionConditionType< decltype(!aPtr.found())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(!aPtr.found()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("!aPtr.found()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2161); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!aPtr.found()" ")"); do { *((volatile int*)__null) = 2161; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
2162 | MOZ_ASSERT(!(aPtr.mKeyHash & sCollisionBit))do { static_assert( mozilla::detail::AssertionConditionType< decltype(!(aPtr.mKeyHash & sCollisionBit))>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(!(aPtr.mKeyHash & sCollisionBit )))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("!(aPtr.mKeyHash & sCollisionBit)", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2162); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!(aPtr.mKeyHash & sCollisionBit)" ")"); do { *((volatile int*)__null) = 2162; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
2163 | ||||
2164 | // Check for error from ensureHash() here. | |||
2165 | if (!aPtr.isLive()) { | |||
2166 | return false; | |||
2167 | } | |||
2168 | ||||
2169 | MOZ_ASSERT(aPtr.mGeneration == generation())do { static_assert( mozilla::detail::AssertionConditionType< decltype(aPtr.mGeneration == generation())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(aPtr.mGeneration == generation ()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("aPtr.mGeneration == generation()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2169); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPtr.mGeneration == generation()" ")"); do { *((volatile int*)__null) = 2169; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
2170 | #ifdef DEBUG1 | |||
2171 | MOZ_ASSERT(aPtr.mMutationCount == mMutationCount)do { static_assert( mozilla::detail::AssertionConditionType< decltype(aPtr.mMutationCount == mMutationCount)>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(! !(aPtr.mMutationCount == mMutationCount))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("aPtr.mMutationCount == mMutationCount" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2171); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPtr.mMutationCount == mMutationCount" ")"); do { *((volatile int*)__null) = 2171; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
2172 | #endif | |||
2173 | ||||
2174 | if (!aPtr.isValid()) { | |||
2175 | MOZ_ASSERT(!mTable && mEntryCount == 0)do { static_assert( mozilla::detail::AssertionConditionType< decltype(!mTable && mEntryCount == 0)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(!mTable && mEntryCount == 0))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("!mTable && mEntryCount == 0", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2175); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!mTable && mEntryCount == 0" ")"); do { *((volatile int*)__null) = 2175; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
2176 | uint32_t newCapacity = rawCapacity(); | |||
2177 | RebuildStatus status = changeTableSize(newCapacity, ReportFailure); | |||
2178 | MOZ_ASSERT(status != NotOverloaded)do { static_assert( mozilla::detail::AssertionConditionType< decltype(status != NotOverloaded)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(status != NotOverloaded))), 0 ))) { do { } while (false); MOZ_ReportAssertionFailure("status != NotOverloaded" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2178); AnnotateMozCrashReason("MOZ_ASSERT" "(" "status != NotOverloaded" ")"); do { *((volatile int*)__null) = 2178; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
2179 | if (status == RehashFailed) { | |||
2180 | return false; | |||
2181 | } | |||
2182 | aPtr.mSlot = findNonLiveSlot(aPtr.mKeyHash); | |||
2183 | ||||
2184 | } else if (aPtr.mSlot.isRemoved()) { | |||
2185 | // Changing an entry from removed to live does not affect whether we are | |||
2186 | // overloaded and can be handled separately. | |||
2187 | if (!this->checkSimulatedOOM()) { | |||
2188 | return false; | |||
2189 | } | |||
2190 | mRemovedCount--; | |||
2191 | aPtr.mKeyHash |= sCollisionBit; | |||
2192 | ||||
2193 | } else { | |||
2194 | // Preserve the validity of |aPtr.mSlot|. | |||
2195 | RebuildStatus status = rehashIfOverloaded(); | |||
2196 | if (status == RehashFailed) { | |||
2197 | return false; | |||
2198 | } | |||
2199 | if (status == NotOverloaded && !this->checkSimulatedOOM()) { | |||
2200 | return false; | |||
2201 | } | |||
2202 | if (status == Rehashed) { | |||
2203 | aPtr.mSlot = findNonLiveSlot(aPtr.mKeyHash); | |||
2204 | } | |||
2205 | } | |||
2206 | ||||
2207 | aPtr.mSlot.setLive(aPtr.mKeyHash, std::forward<Args>(aArgs)...); | |||
2208 | mEntryCount++; | |||
2209 | #ifdef DEBUG1 | |||
2210 | mMutationCount++; | |||
2211 | aPtr.mGeneration = generation(); | |||
2212 | aPtr.mMutationCount = mMutationCount; | |||
2213 | #endif | |||
2214 | return true; | |||
2215 | } | |||
2216 | ||||
2217 | // Note: |aLookup| may reference pieces of arguments in |aArgs|, so this | |||
2218 | // function must take care not to use |aLookup| after moving |aArgs|. | |||
2219 | template <typename... Args> | |||
2220 | void putNewInfallible(const Lookup& aLookup, Args&&... aArgs) { | |||
2221 | MOZ_ASSERT(!lookup(aLookup).found())do { static_assert( mozilla::detail::AssertionConditionType< decltype(!lookup(aLookup).found())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(!lookup(aLookup).found()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("!lookup(aLookup).found()" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2221); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!lookup(aLookup).found()" ")"); do { *((volatile int*)__null) = 2221; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
2222 | ReentrancyGuard g(*this); | |||
2223 | HashNumber keyHash = prepareHash(HashPolicy::hash(aLookup)); | |||
2224 | putNewInfallibleInternal(keyHash, std::forward<Args>(aArgs)...); | |||
2225 | } | |||
2226 | ||||
2227 | // Note: |aLookup| may alias arguments in |aArgs|, so this function must take | |||
2228 | // care not to use |aLookup| after moving |aArgs|. | |||
2229 | template <typename... Args> | |||
2230 | [[nodiscard]] bool putNew(const Lookup& aLookup, Args&&... aArgs) { | |||
2231 | MOZ_ASSERT(!lookup(aLookup).found())do { static_assert( mozilla::detail::AssertionConditionType< decltype(!lookup(aLookup).found())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(!lookup(aLookup).found()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("!lookup(aLookup).found()" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2231); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!lookup(aLookup).found()" ")"); do { *((volatile int*)__null) = 2231; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
2232 | ReentrancyGuard g(*this); | |||
2233 | if (!this->checkSimulatedOOM()) { | |||
2234 | return false; | |||
2235 | } | |||
2236 | HashNumber inputHash; | |||
2237 | if (!EnsureHash<HashPolicy>(aLookup, &inputHash)) { | |||
2238 | return false; | |||
2239 | } | |||
2240 | HashNumber keyHash = prepareHash(inputHash); | |||
2241 | if (rehashIfOverloaded() == RehashFailed) { | |||
2242 | return false; | |||
2243 | } | |||
2244 | putNewInfallibleInternal(keyHash, std::forward<Args>(aArgs)...); | |||
2245 | return true; | |||
2246 | } | |||
2247 | ||||
2248 | // Note: |aLookup| may be a reference pieces of arguments in |aArgs|, so this | |||
2249 | // function must take care not to use |aLookup| after moving |aArgs|. | |||
2250 | template <typename... Args> | |||
2251 | [[nodiscard]] bool relookupOrAdd(AddPtr& aPtr, const Lookup& aLookup, | |||
2252 | Args&&... aArgs) { | |||
2253 | // Check for error from ensureHash() here. | |||
2254 | if (!aPtr.isLive()) { | |||
2255 | return false; | |||
2256 | } | |||
2257 | #ifdef DEBUG1 | |||
2258 | aPtr.mGeneration = generation(); | |||
2259 | aPtr.mMutationCount = mMutationCount; | |||
2260 | #endif | |||
2261 | if (mTable) { | |||
2262 | ReentrancyGuard g(*this); | |||
2263 | // Check that aLookup has not been destroyed. | |||
2264 | MOZ_ASSERT(prepareHash(HashPolicy::hash(aLookup)) == aPtr.mKeyHash)do { static_assert( mozilla::detail::AssertionConditionType< decltype(prepareHash(HashPolicy::hash(aLookup)) == aPtr.mKeyHash )>::isValid, "invalid assertion condition"); if ((__builtin_expect (!!(!(!!(prepareHash(HashPolicy::hash(aLookup)) == aPtr.mKeyHash ))), 0))) { do { } while (false); MOZ_ReportAssertionFailure( "prepareHash(HashPolicy::hash(aLookup)) == aPtr.mKeyHash", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2264); AnnotateMozCrashReason("MOZ_ASSERT" "(" "prepareHash(HashPolicy::hash(aLookup)) == aPtr.mKeyHash" ")"); do { *((volatile int*)__null) = 2264; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
2265 | aPtr.mSlot = lookup<ForAdd>(aLookup, aPtr.mKeyHash); | |||
2266 | if (aPtr.found()) { | |||
2267 | return true; | |||
2268 | } | |||
2269 | } else { | |||
2270 | // Clear aPtr so it's invalid; add() will allocate storage and redo the | |||
2271 | // lookup. | |||
2272 | aPtr.mSlot = Slot(nullptr, nullptr); | |||
2273 | } | |||
2274 | return add(aPtr, std::forward<Args>(aArgs)...); | |||
2275 | } | |||
2276 | ||||
2277 | void remove(Ptr aPtr) { | |||
2278 | MOZ_ASSERT(mTable)do { static_assert( mozilla::detail::AssertionConditionType< decltype(mTable)>::isValid, "invalid assertion condition") ; if ((__builtin_expect(!!(!(!!(mTable))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("mTable", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2278); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mTable" ")" ); do { *((volatile int*)__null) = 2278; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
2279 | ReentrancyGuard g(*this); | |||
2280 | MOZ_ASSERT(aPtr.found())do { static_assert( mozilla::detail::AssertionConditionType< decltype(aPtr.found())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(aPtr.found()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("aPtr.found()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2280); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPtr.found()" ")"); do { *((volatile int*)__null) = 2280; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
2281 | MOZ_ASSERT(aPtr.mGeneration == generation())do { static_assert( mozilla::detail::AssertionConditionType< decltype(aPtr.mGeneration == generation())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(aPtr.mGeneration == generation ()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("aPtr.mGeneration == generation()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2281); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPtr.mGeneration == generation()" ")"); do { *((volatile int*)__null) = 2281; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
2282 | remove(aPtr.mSlot); | |||
2283 | shrinkIfUnderloaded(); | |||
2284 | } | |||
2285 | ||||
2286 | void rekeyWithoutRehash(Ptr aPtr, const Lookup& aLookup, const Key& aKey) { | |||
2287 | MOZ_ASSERT(mTable)do { static_assert( mozilla::detail::AssertionConditionType< decltype(mTable)>::isValid, "invalid assertion condition") ; if ((__builtin_expect(!!(!(!!(mTable))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("mTable", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2287); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mTable" ")" ); do { *((volatile int*)__null) = 2287; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
2288 | ReentrancyGuard g(*this); | |||
2289 | MOZ_ASSERT(aPtr.found())do { static_assert( mozilla::detail::AssertionConditionType< decltype(aPtr.found())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(aPtr.found()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("aPtr.found()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2289); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPtr.found()" ")"); do { *((volatile int*)__null) = 2289; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
2290 | MOZ_ASSERT(aPtr.mGeneration == generation())do { static_assert( mozilla::detail::AssertionConditionType< decltype(aPtr.mGeneration == generation())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(aPtr.mGeneration == generation ()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("aPtr.mGeneration == generation()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2290); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPtr.mGeneration == generation()" ")"); do { *((volatile int*)__null) = 2290; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
2291 | typename HashTableEntry<T>::NonConstT t(std::move(*aPtr)); | |||
2292 | HashPolicy::setKey(t, const_cast<Key&>(aKey)); | |||
2293 | remove(aPtr.mSlot); | |||
2294 | HashNumber keyHash = prepareHash(HashPolicy::hash(aLookup)); | |||
2295 | putNewInfallibleInternal(keyHash, std::move(t)); | |||
2296 | } | |||
2297 | ||||
2298 | void rekeyAndMaybeRehash(Ptr aPtr, const Lookup& aLookup, const Key& aKey) { | |||
2299 | rekeyWithoutRehash(aPtr, aLookup, aKey); | |||
2300 | infallibleRehashIfOverloaded(); | |||
2301 | } | |||
2302 | }; | |||
2303 | ||||
2304 | } // namespace detail | |||
2305 | } // namespace mozilla | |||
2306 | ||||
2307 | #endif /* mozilla_HashTable_h */ |